gpt4 book ai didi

python - 如何使用 Mechanize 捕捉超时

转载 作者:行者123 更新时间:2023-12-04 16:21:09 25 4
gpt4 key购买 nike

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

所以,我使用 python-dbg 来检查发生了什么。我发现在某些网址上,它卡住了。我尝试向 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:

最佳答案

只需添加此 br.set_handle_refresh(False)连同 timeout=0

关于python - 如何使用 Mechanize 捕捉超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57294773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com