Search code examples
pythonpython-3.xgoogle-search

Error with Google search in Python: 503 Service Unavailable


When I try to do in a python console:

from google import search
urls = search("site:facebook.com inurl:login", stop=20)
for url in urls:
    print(url)

In order to search login pages, I obtain an error:

urllib.error.HTTPError: HTTP Error 503: Service Unavailable

However, if I try to search it in Google manually it works, might google be blocking my query?


Solution

  • Google does try to prevent "unexpected" queries from going through. In the normal browser UI it would serve a captcha. It will take into account the traffic pattern (too rapid searches with "smart" queries, IP block known to be used by spammers) and the behavior of the client.

    You can examine the error's detail by catching it.

    try:
        urls = search("site:facebook.com inurl:login", stop=20)
    except urllib.error.HTTPError as httperr:
        print(httperr.headers)  # Dump the headers to see if there's more information
        print(httperr.read())   # You can even read this error object just like a normal response file