【问题标题】:Error with Google search in Python: 503 Service UnavailablePython 中的 Google 搜索出错:503 服务不可用
【发布时间】:2017-08-28 22:19:23
【问题描述】:

当我尝试在 python 控制台中进行操作时:

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

为了搜索登录页面,我得到一个错误:

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

但是,如果我尝试在 Google 中手动搜索它,它会起作用,Google 是否会阻止我的查询?

【问题讨论】:

  • 您的请求中可能缺少适当的用户代理?
  • 我认为它与查询本身有关,因为如果我对查询做同样的事情:例如,“site:facebook.com”,它就可以工作。问题似乎是“inurls”,但我不知道为什么。

标签: python python-3.x google-search


【解决方案1】:

就像Cong Ma 在他的回答中所说,在 google 上进行许多自动搜索会导致 google 阻止你,你会收到错误 503。只有 google 的用于进行当前工作的搜索的 API 是 Google Custom Search API。问题在于它旨在搜索您的页面。并且可以选择将其设置为搜索所有页面(请参阅此answer),但即便如此,您每天也只能有 100 个搜索。之前可以选择使用其他 API,但像 Bing 和 Yahoo 一样,但它们都不再是免费的了。只有进行互联网搜索的免费 API 是 FAROO API。但仍有一种选择可以使用selenium webdriver 进行谷歌搜索。 Selenium 用于模拟浏览器的使用,它有options 来使用 Firefox、Chrome、Edge 或 Safari 网络驱动程序(它实际上会打开 Chrome 并进行搜索),但这很烦人,因为您实际上并不想看到浏览器。但是有一个解决方案,您可以使用PhantomJS。从here下载。提取出来看看下面的例子如何使用(我写了一个简单的类,你可以使用,你只需要改变PhantomJS的路径):

import time
from urllib.parse import quote_plus
from selenium import webdriver


class Browser:

    def __init__(self, path, initiate=True, implicit_wait_time = 10, explicit_wait_time = 2):
        self.path = path
        self.implicit_wait_time = implicit_wait_time    # http://www.aptuz.com/blog/selenium-implicit-vs-explicit-waits/
        self.explicit_wait_time = explicit_wait_time    # http://www.aptuz.com/blog/selenium-implicit-vs-explicit-waits/
        if initiate:
            self.start()
        return

    def start(self):
        self.driver = webdriver.PhantomJS(path)
        self.driver.implicitly_wait(self.implicit_wait_time)
        return

    def end(self):
        self.driver.quit()
        return

    def go_to_url(self, url, wait_time = None):
        if wait_time is None:
            wait_time = self.explicit_wait_time
        self.driver.get(url)
        print('[*] Fetching results from: {}'.format(url))
        time.sleep(wait_time)
        return

    def get_search_url(self, query, page_num=0, per_page=10, lang='en'):
        query = quote_plus(query)
        url = 'https://www.google.hr/search?q={}&num={}&start={}&nl={}'.format(query, per_page, page_num*per_page, lang)
        return url

    def scrape(self):
        #xpath migth change in future
        links = self.driver.find_elements_by_xpath("//h3[@class='r']/a[@href]") # searches for all links insede h3 tags with class "r"
        results = []
        for link in links:
            d = {'url': link.get_attribute('href'),
                 'title': link.text}
            results.append(d)
        return results

    def search(self, query, page_num=0, per_page=10, lang='en', wait_time = None):
        if wait_time is None:
            wait_time = self.explicit_wait_time
        url = self.get_search_url(query, page_num, per_page, lang)
        self.go_to_url(url, wait_time)
        results = self.scrape()
        return results




path = '<YOUR PATH TO PHANTOMJS>/phantomjs-2.1.1-windows/bin/phantomjs.exe' ## SET YOU PATH TO phantomjs
br = Browser(path)
results = br.search('site:facebook.com inurl:login')
for r in results:
    print(r)

br.end()

【讨论】:

    【解决方案2】:

    Google 确实会尝试防止“意外”查询通过。在普通浏览器 UI 中,它将提供验证码。它将考虑流量模式(“智能”查询的搜索过快、垃圾邮件发送者使用的已知 IP 块)和客户端的行为。

    您可以通过捕获来检查错误的详细信息。

    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
    

    【讨论】:

    • 谢谢,我猜是这样,所以现在的问题是,我能避免这种谷歌预防吗?
    • 只有谷歌才能知道。它可能会使用一些 JavaScript 来测试客户端是否是正确的浏览器。在那种情况下,通过这个图灵测试会更加困难。
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2021-05-10
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多