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?
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
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