Questo comando shell riesceHTTP 403 errore durante il recupero robots.txt con mechanize
$ curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 (compatible;)" http://fifa-infinity.com/robots.txt
e stampa robots.txt. L'omissione dell'opzione user-agent provoca un errore 403 dal server. L'ispezione del file robots.txt mostra che il contenuto sotto http://www.fifa-infinity.com/board è consentito per la scansione. Tuttavia, il seguente esito negativo (codice python):
import logging
import mechanize
from mechanize import Browser
ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 (compatible;)'
br = Browser()
br.addheaders = [('User-Agent', ua)]
br.set_debug_http(True)
br.set_debug_responses(True)
logging.getLogger('mechanize').setLevel(logging.DEBUG)
br.open('http://www.fifa-infinity.com/robots.txt')
E l'uscita sulla mia console è:
No handlers could be found for logger "mechanize.cookies"
send: 'GET /robots.txt HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.fifa-infinity.com\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 (compatible;)\r\n\r\n'
reply: 'HTTP/1.1 403 Bad Behavior\r\n'
header: Date: Wed, 13 Feb 2013 15:37:16 GMT
header: Server: Apache
header: X-Powered-By: PHP/5.2.17
header: Vary: User-Agent,Accept-Encoding
header: Connection: close
header: Transfer-Encoding: chunked
header: Content-Type: text/html
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/moshev/Projects/forumscrawler/lib/python2.7/site-packages/mechanize/_mechanize.py", line 203, in open
return self._mech_open(url, data, timeout=timeout)
File "/home/moshev/Projects/forumscrawler/lib/python2.7/site-packages/mechanize/_mechanize.py", line 255, in _mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 403: Bad Behavior
Stranamente, utilizzando riccio senza impostare i risultati user-agent in "403: Forbidden" piuttosto di "403: cattivo comportamento".
Sono in qualche modo facendo qualcosa di sbagliato, o si tratta di un bug in mechanize/urllib2? Non vedo come ottenere semplicemente robots.txt può essere un "cattivo comportamento"?
E un altro esempio di intestazione sniffing andato a male. Il server dall'altra parte guarda più all'agente UA, controlla quali intestazioni 'curl' sta inviando, confrontale con quello che' mechanize' sta usando, aggiusta, rince, ripeti. Questo non è * un problema python. –
Questo problema sembra molto simile a [urllib2.HTTPError: HTTP Error 403: Forbidden] (https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden/46213623#46213623) – djinn