2015-02-08 18 views
17

Próbuję otworzyć i parsować stronę html. W pythonie 2.7.8 nie mam problemu:Python 3.4 urllib.request error (http 403)

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

i wszystko jest w porządku. Jednak chcę przenieść do Pythona 3.4 i tam dostaję błąd HTTP 403 (zabronione). Mój kod:

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 

Działa dla innych adresów URL, które nie używają protokołu HTTPS.

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

jest w porządku.

+0

Zobacz również https://stackoverflow.com/questions/3336549/pythons-urllib2-why-do-i-get-error -403-when-i-urlopen-a-wikipedia-page – Trilarion

Odpowiedz

27

Wygląda na to, że strona nie lubi agenta użytkownika w Pythonie 3.x.

Określanie User-Agent rozwiąże problemu:

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

UWAGA Python w wersji 2.x urllib otrzyma również 403 status, ale w przeciwieństwie do Pythona 2.x urllib2 i Python 3.x urllib, to nie budzi wyjątek.

Można potwierdzić, że następujący kod:

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

Dzięki. Zadziałało! – Belial

+0

dzięki! pracował dla mnie też – DenisFLASH

+0

nie działa .. nadal zabronione – Martian2049

0

Oto niektóre notatki zebrałem na urllib kiedy studiowałem python-3:
trzymałem je w przypadku mogłyby się przydać lub komuś pomóc w przeciwnym razie.

Jak importować urllib.request i urllib.parse:

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

Jak złożyć zamówienie otrzymasz:

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

Jak złożyć zamówienie postu:

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() 

jak zrobić Żądanie POST (403 forbidden odpowiedzi):

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() 

Jak żądania GET (403 forbidden wskazań):

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

Przepraszam za błędy w mojej starej odpowiedzi, zrobiłem trochę badań i naprawiłem te. Te błędy zainspirowały mnie do powrotu i sprawdzenia, czy moje notatki są poprawne: D –

+0

Dlaczego działa drugi link bez żadnego problemu? – Sudheer1990

Powiązane problemy