跳到正文
DEV Community· the bomber·· 3 小時前AI 評分24

如何使用 PipeWire 和 Tesseract OCR 自動追蹤本地 SEO 排名

Automating Local SEO Tracking with PipeWire and Tesseract OCR

AI 導讀

作者使用 PipeWire 擷取瀏覽器中的本地地圖搜尋畫面,再以 Tesseract OCR 讀取商戶排名和競爭對手名稱,建立本地 SEO 追蹤工具。Python 腳本串連瀏覽器、畫面擷取和 OCR,並根據商戶名稱是否出現在擷取文字中判斷其排名位置。工具在本地硬件上執行,無須支付 SaaS 月費。

正文

Why Track Local Map Rankings Visually

Running a local service website in Palm Beach County means your visibility in local search results dictates your pipeline. Traditional SEO rank trackers often miss hyper-local shifts or get blocked by anti-bot measures when checking map pack positions for specific neighborhoods from a data center IP.

At Dream Futures Tech in Pompano Beach, I run my own local SEO dashboards and automation scripts on a Linux server at home. Instead of paying for expensive enterprise rank-tracking APIs that frequently fail, I built a lightweight local monitor. It opens a browser, captures the screen using PipeWire, and uses Tesseract OCR to read my business's position and competitor names directly from the rendered pixels.

In my judgment, relying on visual screen captures for localized data is slower than an API, but it is entirely immune to API deprecations and accurately reflects what a real user sees on a browser screen.

Capturing the Screen via PipeWire

On modern Linux desktops running Wayland, capturing the screen securely requires PipeWire. While desktop environments offer GUI prompts for screen sharing, an automation server needs a headless or scripted approach. I use pw-cat and related PipeWire utilities alongside a lightweight browser instance to grab exact viewport frames.

First, make sure you have the necessary system packages installed:

sudo apt install tesseract-ocr pipewire pipewire-bin python3-pip
pip install pytesseract pillow

When the automation script triggers a browser window to query local search results, it targets a specific window ID or desktop region. For simplicity in my home server setup, I capture a designated region of the framebuffer where the map results render.

Extracting Text with Tesseract OCR

Once the image is saved to disk by the capture routine, Tesseract processes the image to pull out the text. Local maps use varying fonts and map overlays that can confuse naive OCR setups. To get reliable reads, preprocessing the image with Python's Pillow library helps immensely.

Here is a snippet of the core OCR function I use to parse the captured screenshot:

import pytesseract
from PIL import Image, ImageOps

def extract_rankings(image_path):
    # Open the captured screen region
    img = Image.open(image_path)

    # Convert to grayscale and increase contrast
    gray = ImageOps.grayscale(img)
    contrast = ImageOps.autocontrast(gray, cutoff=2)

    # Run Tesseract with custom config for single blocks of text
    custom_config = r'--oem 3 --psm 6'
    text = pytesseract.image_to_string(contrast, config=custom_config)

    return text

# Example usage
results = extract_rankings('/tmp/map_capture.png')
print(results)

In practice, Tesseract handles clean sans-serif UI fonts quite well, provided the scaling factor of your browser zoom is consistent across runs.

Putting It All Together in a Python Script

To automate the full check, the script orchestrates the browser launch, waits for the map render to complete, triggers the PipeWire snapshot, and runs the OCR parser. If my business name appears in the extracted text block, it logs the vertical coordinate offset to determine the exact rank position.

import subprocess
import time

def capture_and_read():
    # Trigger browser or use existing session
    # Capture screen region using PipeWire-backed tool
    subprocess.run([
        "grim", "-g", "100,200 800,600", "/tmp/map_capture.png"
    ])

    # Parse text
    extracted_text = extract_rankings('/tmp/map_capture.png')

    if "Dream Futures Tech" in extracted_text:
        print("Business found in current viewport.")
    else:
        print("Business not found in immediate view; checking scroll bounds.")

if __name__ == "__main__":
    capture_and_read()

This setup runs locally on my hardware without leaking telemetry or incurring monthly SaaS fees. It gives me honest, ground-truth data on how local service searches render right here in South Florida.

I build sites and automations for small businesses at dreamfuturestech.com.

來源:DEV Community · dev.to