2015-02-08 23 views
17

Tôi đang cố gắng để mở và phân tích một trang html. Trong python 2.7.8 Tôi không có vấn đề:Python 3.4 lỗi urllib.request (http 403)

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

và mọi thứ đều tốt. Tuy nhiên tôi muốn chuyển sang python 3.4 và ở đó tôi nhận được lỗi HTTP 403 (Cấm). Mã của tôi:

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 

Làm việc cho các URL khác không sử dụng https.

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

là ok.

+0

Xem thêm https://stackoverflow.com/questions/3336549/pythons-urllib2-why-do-i-get-error -403-khi-i-urlopen-a-wikipedia-trang – Trilarion

Trả lời

27

Có vẻ như trang web không thích tác nhân người dùng của Python 3.x.

Xác định User-Agent sẽ giải quyết vấn đề của bạn:

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

LƯU Ý Python phiên bản 2.x urllib cũng nhận được 403 trạng thái, nhưng không giống như Python 2.x urllib2 và Python 3.x urllib, nó không làm tăng sự ngoại lệ.

Bạn có thể xác nhận rằng bằng đoạn mã sau:

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

Cảm ơn. Nó đã làm việc! – Belial

+0

cảm ơn! làm việc cho tôi quá – DenisFLASH

+0

không làm việc .. vẫn cấm – Martian2049

0

Dưới đây là một số lưu ý tôi tụ tập trên urllib khi tôi đang học python-3:
tôi giữ họ trong trường hợp họ có thể có ích hoặc giúp đỡ một ai đó khác.

Làm thế nào để nhập khẩu urllib.requesturllib.parse:

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

Làm thế nào để thực hiện một yêu cầu GET:

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

Làm thế nào để thực hiện một yêu cầu 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() 

Làm thế nào để thực hiện một Yêu cầu POST (403 forbidden câu trả lời):

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

Làm thế nào để thực hiện một yêu cầu GET (403 forbidden phản ứng):

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

Tôi xin lỗi vì các lỗi trong câu trả lời cũ của tôi, tôi đã thực hiện một số nghiên cứu và khắc phục những lỗi đó. Những lỗi đó đã truyền cảm hứng cho tôi quay trở lại và kiểm tra xem các ghi chú của tôi có chính xác không: D –

+0

Tại sao nó lại hoạt động cho liên kết thứ hai mà không có bất kỳ vấn đề gì? – Sudheer1990

Các vấn đề liên quan