2014-11-12 29 views
6

Hy! Ho provato ad aprire la pagina web, che normalmente si apre nel browser, ma python giura e non vuole funzionare.Ancora urllib.error.HTTPError: Errore HTTP 400: Richiesta errata

import urllib.request, urllib.error 
f = urllib.request.urlopen('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire') 

e un altro modo

import urllib.request, urllib.error 
opener=urllib.request.build_opener() 
f=opener.open('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphi 
re') 

Entrambe le opzioni offrono un tipo di errore:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python34\lib\urllib\request.py", line 461, in open 
    response = meth(req, response) 
    File "C:\Python34\lib\urllib\request.py", line 571, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python34\lib\urllib\request.py", line 493, in error 
    result = self._call_chain(*args) 
    File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain 
    result = func(*args) 
    File "C:\Python34\lib\urllib\request.py", line 676, in http_error_302 
    return self.parent.open(new, timeout=req.timeout) 
    File "C:\Python34\lib\urllib\request.py", line 461, in open 
    response = meth(req, response) 
    File "C:\Python34\lib\urllib\request.py", line 571, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python34\lib\urllib\request.py", line 499, in error 
    return self._call_chain(*args) 
    File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain 
    result = func(*args) 
    File "C:\Python34\lib\urllib\request.py", line 579, in http_error_default 
    raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 400: Bad Request 

Tutte le idee?

risposta

1

Questo URL sembra fare stringa user agent controllo. Se aggiusto la stringa del mio agente utente in Firefox a Python-urllib/2.7, fallisce con lo Bad Request che stai vedendo.

Come si utilizza urllib, è possibile regolare l'User Agent seguendo questo tutorial

from urllib.request import FancyURLopener 

class MyOpener(FancyURLopener): 
    version = 'My new User-Agent' # Set this to a string you want for your user agent 

myopener = MyOpener() 
page = myopener.open('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire') 
+0

Grazie, ho cambiato 'da urllib import FancyURLopener' a 'da urllib.request import FancyURLopener' (era un errore). E alla fine ho un errore successivo (dopo aver eseguito '>>> page.read()'): ValueError: lettura del file chiuso. – Wanu

+0

Quindi, ho modificato version = 'My new User-Agent' in version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv: 1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'. E l'errore è scomparso! Grazie mille! Sono stato alla ricerca di una soluzione a questo problema per molto tempo, mi hai aiutato molto! – Wanu

2

Probabilmente stanno bloccando il fatto che non proviene da un browser. Probabilmente hai bisogno di un'intestazione User-Agent valida o qualcosa del genere.

Utilizzando le richieste, questo funziona:

import requests 
headers = 
{ 
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/37.0.2049.0 Safari/537.36' 
} 

r = requests.get('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire', headers=headers) 
print r 
print r.headers 
+0

Wow, questo è sicuramente la risposta giusta per tutti coloro che utilizzando la libreria 'requests'! Salvato il mio bacon! – Blairg23

Problemi correlati