gpt4 book ai didi

Python RoboBrowser SSL 错误 : bad handshake: SysCallError(104, 'ECONNRESET' )

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:23 25 4
gpt4 key购买 nike

我正在尝试在网页上填写表格并使用 RoboBrowser 库返回一些结果。

我有一个包含 ~200k 引用的文件可能无法提供足够的答案(数据 [1] 中存储的街道名称可能与表单中所需的不同,并导致打开另一个页面)。如果响应不是预期的(主要是由于街道名称错误),我的代码会运行并打印我想要获取的信息或打印“NS”。

但是,经过一段随机时间后(起初大约 1300 次循环,然后大约 100-300 次或更少),我得到:

    Traceback (most recent call last):
File "web_scraper.py", line 49, in <module>
result = rechCadastre(data_point,result)
File "web_scraper.py", line 16, in rechCadastre
browser.submit_form(form)
File "/usr/local/lib/python2.7/dist-packages/robobrowser/browser.py", line 343, in submit_form
response = self.session.request(method, url, **send_args)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: SysCallError(104, 'ECONNRESET')",)

我尝试在各处添加 time.sleep(0.01),以为我会使页面重载,但它没有帮助。有人有想法吗?

我的代码:

import csv
import re
from robobrowser import RoboBrowser
import time

def rechCadastre(data,result):
time.sleep(0.01)
form=browser.get_form(id="rech")
repetition = ''
if data[2]!='':
repetition = data[2][0]
param={'numeroVoie':data[1],'nomVoie':data[0],'ville':data[3], 'indiceRepetition':repetition}
for x in iter(param):
form[x]=param[x]
time.sleep(0.01)
browser.submit_form(form)
success = browser.select('#onglet')
if not success:
result += "NS,NS"
else:
answer = browser.select('.nomcol')[0]
parcelle= re.split('(\W+)',re.search("Parcelle n\W [0-9]{1,4}",answer.text).group(0))[4]
feuille = re.split('(\W+)',re.search("Feuille [0-9]{1,4} [A-Z]{1,4}",answer.text).group(0))[4]
result += feuille+","+parcelle
browser.back()
return result


data = []
url = "https://www.cadastre.gouv.fr/scpc/accueil.do"
browser = RoboBrowser()
browser.open(url)

infile = open("treated.csv",'rb')
reader=csv.reader(infile)
for row in reader:
data.append(row)

#compt=0
for data_point in data:
# if compt == 20:
# break
# data_point = data[i]
result = data_point[0] + "," + data_point[1] + "," + data_point[2] + "," + data_point[3] + ",,"
nd = data_point[0] == "#N/D"
rep = (data_point[2] == '') or (data_point[2] == 'BIS') or (data_point[2] == 'TER') or (data_point[2] == 'QUATER') and (data_point[2] == 'B')
acceptable = rep and (not nd)
if acceptable:
result = rechCadastre(data_point,result)
print result
# compt += 1

我正在使用 Ubuntu 16.04.2 LTS 和 Python2.7

谢谢!

最佳答案

我设法通过将每次调用的等待时间 time.sleep 更改为 1 来避免错误。该程序有时会再次出错,但实际上很少(大约在 20000-30000 个请求中出现一次)。

感谢您的支持!

关于Python RoboBrowser SSL 错误 : bad handshake: SysCallError(104, 'ECONNRESET' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42732784/

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