python 的谷歌搜索库

googlesearch library for python

我有一个很大的查询文件,需要在 google 中搜索,并且 return 在另一个文件中搜索该查询的结果 URL。我正在使用这个包 https://pypi.python.org/pypi/googlesearch/0.7.0

当 运行在终端中运行我的程序时,我经常将 20 个左右的 URL 打印到屏幕上,然后我将 GoogleSearch 切换到代理编号 1 打印到屏幕上。几分钟后,一长串错误以建立新连接失败结束:[Errno 60] 操作超时'打印到屏幕上。

我的问题是,为什么我先得到结果,然后代理号码切换?我怀疑我向 google 发送了太多请求,但有时当我 运行 我的程序时,我可能会得到 4 个结果,然后我得到相同的消息和错误。我能做什么?在包中有一个文件 search.py 决定使用哪个代理或何时更改。如果这是错误的原因,是否可以调整请求之间的时间量?

search.py的重要部分

def proxy(self):
    if self.use_proxy:
        return {"http": settings.PROXY_LIST[self.proxy_no]}
    else:
        return {"http": None}

def switch_to_next_proxy(self):
    num_proxies = len(settings.PROXY_LIST)
    GoogleSearch.proxy_no = (self.proxy_no + 1) % num_proxies
    if self.verbose:
        print >> sys.stderr, ('GoogleSearch switched to '
                              'proxy number %i' % self.proxy_no)

在连续的搜索请求后,Google 会怀疑访问机器人并获取验证码验证。

googlesearch 实现了代理的自动切换以解决该问题:

  • use_proxy: bool, default: True If True, GoogleSearch will use the proxies defined in the PROXIES_LIST variable of googlesearch_settings.py to do the searches. If a proxy starts getting HTTP 403 FORBIDDEN responses, it will switch to the next proxy in the list. It will raise a GoogleAPIError only if all proxies get 403 responses.

您应该使用 tor 网络或任何 vpn 网络创建许多代理,并将它们添加到 googlesearch_settings.py

PROXIES_LIST 变量中