2

I am trying to fill out a form on a web page and get some of the results back using the RoboBrowser library.

I have a file with ~200k references that may not give the adaquate anwser (the street name stored in data[1] may be different from the one required in the form and cause another page to open). My code runs and either prints the information I wanted to get or prints "NS" if the response is not what is expected (mainly due to the street name being wrong).

However, after a random time (at first after about 1300 cycles, then about 100-300 or less), I get :

    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')",)

I tried adding time.sleep(0.01) here and there, thinking I my be overloading the page, but it didn't help. Does anyone have an idea ?

My code :

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

I am using Ubuntu 16.04.2 LTS and Python2.7

Thank you !

Clement
  • 61
  • 1
  • 1
  • 4
  • ECONNRESET means that the server has closed the connection the hard way. It might be that they block your IP address after a while because of abuse or simply allow only a maximum number of connections within a specific time frame from the same IP address. – Steffen Ullrich Mar 11 '17 at 08:05
  • Thanks @SteffenUllrich ! Is there any way to bypass that ? – Clement Mar 11 '17 at 10:33
  • If the filter is really based on restricting number of requests from the same IP address within a specific time It might help if you limit the number of requests per time or if you access the site from different IP addresses. – Steffen Ullrich Mar 11 '17 at 11:08

1 Answers1

4

I managed to avoid the error by changing the waiting time time.sleep to 1 for each call. The programme sometimes gets the error again, but really rarely (about once in 20000-30000 requests).

Thank you for your support !

Clement
  • 61
  • 1
  • 1
  • 4