2015-02-08 14 views
17

Sto tentando di aprire e analizzare una pagina html. In Python 2.7.8 non ho alcun problema:Python 3.4 urllib.request error (http 403)

import urllib 
url = "https://ipdb.at/ip/66.196.116.112" 
html = urllib.urlopen(url).read() 

e tutto va bene. Comunque voglio passare a Python 3.4 e lì ottengo l'errore HTTP 403 (Proibito). Il mio codice:

import urllib.request 
html = urllib.request.urlopen(url) # same URL as before 

File "C:\Python34\lib\urllib\request.py", line 153, in urlopen 
return opener.open(url, data, timeout) 
File "C:\Python34\lib\urllib\request.py", line 461, in open 
response = meth(req, response) 
File "C:\Python34\lib\urllib\request.py", line 574, 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 582, in http_error_default 
raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 403: Forbidden 

Funziona per altri URL che non utilizzano https.

url = 'http://www.stopforumspam.com/ipcheck/212.91.188.166' 

è ok.

+0

Vedi anche https://stackoverflow.com/questions/3336549/pythons-urllib2-why-do-i-get-error -403-quando-i-urlopen-a-wikipedia-page – Trilarion

risposta

27

Sembra che al sito non piaccia l'agente utente di Python 3.x.

Specificando User-Agent risolverà il vostro problema:

import urllib.request 
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) 
html = urllib.request.urlopen(req).read() 

NOTA Python versione 2.x urllib riceve anche 403 di stato, ma a differenza di Python 2.x urllib2 e Python 3.x urllib, che non solleva l'eccezione.

È possibile confermare che dal seguente codice:

print(urllib.urlopen(url).getcode()) # => 403 
+0

Grazie. Ha funzionato! – Belial

+0

grazie! ha funzionato anche per me – DenisFLASH

+0

non funziona .. ancora vietato – Martian2049

0

Ecco alcune note che ho raccolto su urllib quando studiavo python-3:
Li ho tenuti nel caso in cui potrebbe tornare utile o aiutare qualcuno altrimenti fuori.

Come importare urllib.request e urllib.parse:

import urllib.request as urlRequest 
import urllib.parse as urlParse 

Come fare una richiesta GET:

url = "http://www.example.net" 
# open the url 
x = urlRequest.urlopen(url) 
# get the source code 
sourceCode = x.read() 

Come fare una richiesta POST:

url = "https://www.example.com" 
values = {"q": "python if"} 
# encode values for the url 
values = urlParse.urlencode(values) 
# encode the values in UTF-8 format 
values = values.encode("UTF-8") 
# create the url 
targetUrl = urlRequest.Request(url, values) 
# open the url 
x = urlRequest.urlopen(targetUrl) 
# get the source code 
sourceCode = x.read() 

Come fare un Richiesta POST (risposte 403 forbidden):

url = "https://www.example.com" 
values = {"q": "python urllib"} 
# pretend to be a chrome 47 browser on a windows 10 machine 
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"} 
# encode values for the url 
values = urlParse.urlencode(values) 
# encode the values in UTF-8 format 
values = values.encode("UTF-8") 
# create the url 
targetUrl = urlRequest.Request(url = url, data = values, headers = headers) 
# open the url 
x = urlRequest.urlopen(targetUrl) 
# get the source code 
sourceCode = x.read() 

Come fare una richiesta GET (403 forbidden risposte):

url = "https://www.example.com" 
# pretend to be a chrome 47 browser on a windows 10 machine 
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"} 
req = urlRequest.Request(url, headers = headers) 
# open the url 
x = urlRequest.urlopen(req) 
# get the source code 
sourceCode = x.read() 
+0

Mi scuso per gli errori nella mia vecchia risposta, ho fatto qualche ricerca e li ho sistemati. Questi errori mi hanno ispirato a tornare indietro e controllare se le mie note sono tutte corrette: D –

+0

Perché funziona per il secondo link senza problemi? – Sudheer1990