2014-11-12 16 views
6

Hy! Tôi đã cố gắng mở trang web, thường mở trong trình duyệt, nhưng python chỉ thề và không muốn làm việc.Một lần nữa urllib.error.HTTPError: Lỗi HTTP 400: Yêu cầu Không hợp lệ

import urllib.request, urllib.error 
f = urllib.request.urlopen('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire') 

Và một cách khác

import urllib.request, urllib.error 
opener=urllib.request.build_opener() 
f=opener.open('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphi 
re') 

Cả hai tùy chọn cho một loại lỗi:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python34\lib\urllib\request.py", line 461, in open 
    response = meth(req, response) 
    File "C:\Python34\lib\urllib\request.py", line 571, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python34\lib\urllib\request.py", line 493, in error 
    result = 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 676, in http_error_302 
    return self.parent.open(new, timeout=req.timeout) 
    File "C:\Python34\lib\urllib\request.py", line 461, in open 
    response = meth(req, response) 
    File "C:\Python34\lib\urllib\request.py", line 571, 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 579, in http_error_default 
    raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 400: Bad Request 

Bất kỳ ý tưởng?

Trả lời

1

URL này có vẻ là làm tác nhân người dùng chuỗi kiểm tra. Nếu tôi điều chỉnh chuỗi tác nhân người dùng của mình trong Firefox thành Python-urllib/2.7, nó không thành công với số Bad Request bạn đang xem.

Như bạn đang sử dụng urllib, bạn có thể điều chỉnh User Agent sau này tutorial

from urllib.request import FancyURLopener 

class MyOpener(FancyURLopener): 
    version = 'My new User-Agent' # Set this to a string you want for your user agent 

myopener = MyOpener() 
page = myopener.open('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire') 
+0

Cảm ơn bạn, chỉ có tôi đã thay đổi 'từ urllib nhập FancyURLopener' thành 'từ urllib.request import FancyURLopener' (là lỗi). Và cuối cùng tôi có lỗi tiếp theo (sau khi chạy '>>> page.read()'): ValueError: đọc tệp đã đóng. – Wanu

+0

Vì vậy, tôi đã thay đổi phiên bản = 'Tác nhân người dùng mới của tôi' thành phiên bản = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nó; rv: 1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'. Và lỗi đã biến mất! Rất cảm ơn! Tôi đã tìm kiếm một giải pháp cho vấn đề này trong một thời gian dài, bạn đã giúp tôi rất nhiều! – Wanu

2

Có thể họ đang chặn thực tế là nó không đến từ trình duyệt. Bạn có thể cần một tiêu đề Tác nhân người dùng hợp lệ hoặc một cái gì đó.

Sử dụng yêu cầu, công trình này:

import requests 
headers = 
{ 
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/37.0.2049.0 Safari/537.36' 
} 

r = requests.get('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire', headers=headers) 
print r 
print r.headers 
+0

Wow, điều này chắc chắn là câu trả lời đúng cho bất cứ ai sử dụng 'Thư viện requests'! Lưu thịt xông khói của tôi! – Blairg23

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