2

我正在尝试在网页上填写表格并使用 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

谢谢 !

4

1 回答 1

4

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

谢谢您的支持 !

于 2017-03-18T06:05:07.543 回答