DEV Community

Cover image for How to scrape google search results using python with stealth
SerpApi.Org
SerpApi.Org

Posted on Originally published at serpapi.org

How to scrape google search results using python with stealth

Many developers rely on basic setups with BeautifulSoup and random User-Agents, assuming this is enough to navigate modern search engine protections. After a decade in the industry, I have learned that this approach is rarely sufficient. Between TLS fingerprinting and HTTP/2 handshakes, basic scripts are flagged almost instantly.

Here is a breakdown of how to build a robust, production-grade search data pipeline.

The Modern Tech Stack

Stop using requests and BeautifulSoup. They are too slow and easily detected.

  • Use httpx for HTTP/2: Modern security engines analyze your TLS handshake. httpx supports HTTP/2, which allows you to multiplex requests and match the connection signature of a legitimate browser, reducing the likelihood of being blocked.
  • Switch to XPath: CSS selectors are fragile because Google frequently updates class names during A/B tests. parsel with XPath targeting structural elements—like //h3 or specific semantic containers—is much more resilient than brittle CSS selectors.
  • Asynchronous Processing: Use asyncio to fetch results in parallel. This significantly increases throughput while keeping your footprint low.

Handling Detection and Evasion

The most common reason for failure is a mismatch between your headers and your TLS signature.

  • TLS Fingerprinting: Security systems look for a "JA3 signature." If you declare a Chrome User-Agent but the handshake indicates a Python urllib client, you will get hit with a CAPTCHA. Ensure your client negotiates ciphers that align with the browser version you are spoofing.
  • Residential Proxies: Datacenter IPs are often blacklisted entirely. Use a rotating pool of residential proxies. These IPs route your traffic through legitimate consumer ISP nodes, making your requests indistinguishable from real users.

Managing Scale

If your project requires high-volume data (e.g., thousands of keywords per day), maintaining your own infrastructure becomes a burden. You will spend more time fixing broken parsers and managing proxy bans than actually analyzing data.

Task Self-Built Approach Managed Service (e.g., SerpApi)
Maintenance Constant (fix broken XPaths) Zero
CAPTHCAs You must solve them Handled automatically
Output Raw HTML (requires parsing) Clean, structured JSON
Cost Proxy fees + DevOps hours Predictable usage-based pricing

When to Use Headless Browsers

Playwright with the stealth plugin is powerful, but use it sparingly. It is necessary for capturing dynamic content like Maps or "People Also Ask" sections that rely on JavaScript execution. However, it is resource-intensive, consuming roughly 150MB of RAM per instance. Use standard httpx requests for static SERPs and save Playwright for complex, interactive components.

Final Takeaway

For small-scale experiments, a custom httpx + parsel setup is excellent. However, as your volume grows, the overhead of proxy rotation and infrastructure maintenance becomes a liability. If you find yourself spending more than a few hours a week patching your scraper, it is time to shift to a managed API. This lets you focus on building features rather than fighting search engine security.


Originally published at How to scrape google search results using python with stealth

Top comments (0)