2015-02-08 7 views
17

Ich versuche, eine HTML-Seite zu öffnen und zu analysieren. In Python 2.7.8 habe ich kein Problem:Python 3.4 urllib.request Fehler (http 403)

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

und alles ist in Ordnung. Ich möchte jedoch zu Python 3.4 wechseln und dort bekomme ich den HTTP-Fehler 403 (Forbidden). Mein Code:

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 

Es funktioniert für andere URLs, die https nicht verwenden.

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

ist in Ordnung.

+0

Siehe auch https://stackoverflow.com/questions/3336549/pythons-urllib2-why-do-i-get-error -403-when-i-urlopen-a-wikipedia-page – Trilarion

Antwort

27

Es scheint, als ob die Seite den Benutzeragent von Python 3.x nicht mag.

User-Agent Angeben lösen Ihr Problem:

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

HINWEIS Python 2.x urllib Version erhält auch 403-Status, aber im Gegensatz zu Python 2.x urllib2 und Python 3.x urllib, ist es nicht erhöhen die Ausnahme.

Sie können, dass durch folgenden Code bestätigen:

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

Danke. Es funktionierte! – Belial

+0

danke! arbeitete auch für mich – DenisFLASH

+0

funktioniert nicht .. immer noch verboten – Martian2049

0

Hier sind einige Notizen, die ich auf urllib versammelt, als ich studierte Python-3:
Ich hielt sie für den Fall, sie könnten sich als nützlich oder jemand helfen sonst raus.

Wie importieren urllib.request und urllib.parse:

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

Wie eine GET-Anfrage zu machen:

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

Wie eine POST-Anforderung machen:

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

Wie ein machen POST-Anfrage (403 forbidden Antworten):

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

Wie eine GET-Anfrage machen (403 forbidden Antworten):

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

Ich entschuldige mich für die Fehler in meiner alten Antwort, ich habe etwas recherchiert und diese behoben. Diese Fehler haben mich inspiriert, zurückzugehen und zu überprüfen, ob meine Notizen korrekt sind: D –

+0

Warum funktioniert es für die zweite Verbindung ohne irgendein Problem? – Sudheer1990