【问题标题】:How to catch Timeout with mechanize如何用机械化捕捉超时
【发布时间】:2019-07-31 15:56:40
【问题描述】:

我有一个带有 mechanize 的 pythonscript,可以从文本文件中读取 url 列表。大约有 6000 个网址。当它达到 3000 左右时,脚本会挂起。当我看到脚本使用 0% 的处理能力时,我注意到了这一点。

所以,我使用 python-dbg 来检查发生了什么。我发现在某个网址上,它卡住了。我尝试在 url 获取行中添加超时,但它不起作用。

那么有什么办法,如果加载需要这么长时间,我可以添加超时并继续下一个 url?

from json import loads
import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]

with open('mf.txt') as f:
    for line in f:
        try:
            google_results = br.open("https://www.abcd.com" + str(line), timeout=0).read()
            json_obj = loads(google_results)
            if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
                print str(line), "Good"
            else:
                print str(line), "Bad"


        except Exception as e:

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    只需将此br.set_handle_refresh(False)timeout=0 一起添加

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多