【问题标题】:googlesearch library for python用于 python 的谷歌搜索库
【发布时间】:2015-12-19 20:01:01
【问题描述】:

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

在终端中运行我的程序时,我经常会得到 20 个左右的 URL 打印到屏幕上,然后我将 GoogleSearch 切换到代理编号 1 打印到屏幕上。几分钟后,屏幕上会出现一长串以 Failed toestablish a new connection: [Errno 60] Operation timed out' 结尾的错误列表。

我的问题是为什么我首先得到结果然后代理号码切换?我怀疑我向谷歌发送了太多请求,但有时当我运行我的程序时,我可能会得到 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)

【问题讨论】:

    标签: python proxy timeout google-search


    【解决方案1】:

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

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

    • use_proxy:bool,默认值:True 如果为 True,GoogleSearch 将使用在 PROXIES_LIST 变量中定义的代理 googlesearch_settings.py 进行搜索。如果代理开始获取 HTTP 403 FORBIDDEN 响应,它将切换到下一个代理 列表。仅当所有代理都获得 403 时才会引发 GoogleAPIError 回复。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2016-11-22
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多