Python 3.4 urllib.request Fehler (http 403)
Ich versuche zu öffnen und zu Parsen einer html-Seite. In python 2.7.8 habe ich kein problem:
import urllib
url = "https://ipdb.at/ip/66.196.116.112"
html = urllib.urlopen(url).read()
und alles ist fein. Allerdings möchte ich zu bewegen, um python 3.4 und dort bekomme ich den HTTP-Fehler 403 (Verboten). 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 Arbeit für andere URLs, die nicht https verwenden.
url = 'http://www.stopforumspam.com/ipcheck/212.91.188.166'
ist ok.
Kommentar zu dem Problem - Öffnen
Siehe auch stackoverflow.com/questions/3336549/...
InformationsquelleAutor der Frage Belial | 2015-02-08
Du musst angemeldet sein, um einen Kommentar abzugeben.
Es scheint, dass die Website nicht wie der user-agent von Python 3.x.
Angabe
User-Agent
wird Ihr problem lösen:HINWEIS Python-2.x das urllib-version erhält auch den 403-status, aber im Gegensatz zu Python 2.x urllib2 und Python 3.x das urllib, heben Sie nicht die Ausnahme.
Können Sie bestätigen, dass Sie mit folgenden code:
InformationsquelleAutor der Antwort falsetru
Hier sind einige Notizen, die ich gesammelt auf
urllib
wenn ich befasste mich mit python-3:Ich hielt Sie in Fall, dass Sie in handliches kommen könnte oder jemand anderes helfen.
Importieren
urllib.request
undurllib.parse
:, Wie Sie eine GET-Anforderung:
, Wie man eine POST-Anfrage:
Wie Sie eine POST-Anforderung (
403 forbidden
Antworten):, Wie Sie eine GET-Anforderung (
403 forbidden
Antworten):InformationsquelleAutor der Antwort