簡體   English   中英

Python RoboBrowser SSL錯誤:握手錯誤:SysCallError(104,'ECONNRESET')

[英]Python RoboBrowser SSL Error : bad handshake: SysCallError(104, 'ECONNRESET')

我正在嘗試在網頁上填寫表格,並使用RoboBrowser庫獲取一些結果。

我有一個帶有〜200k引用的文件,可能無法提供足夠的數據(存儲在data [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個請求中出現一次)。

謝謝您的支持 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM