2014-11-12 5 views
6

Hy! 브라우저에서 열리는 웹 페이지를 열려고했지만 파이썬은 단지 맹세하고 작동하지 않으려 고합니다. 다시 urllib.error.HTTPError : HTTP 오류 400 : 잘못된 요청

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

그리고

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

두 옵션은 오류의 한 종류 제공하는 또 다른 방법 :

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 

어떤 아이디어?

답변

1

이 URL은 확인 사용자 에이전트 문자열을하고있는 것 같다. Firefox에서 내 사용자 에이전트 문자열을 Python-urllib/2.7으로 조정하면 표시되는 Bad Request으로 실패합니다. 당신이 urllib를 사용하는 것처럼

, 당신은 다음과 같은 사용자 에이전트를 조정할 수있는이 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

고맙습니다 만, 'from urllib import FancyURLopener'를 'urllib.request import FancyURLopener'(오류였습니다)로 변경했습니다. 그리고 결국에는 다음 오류가 발생합니다 ('>>> page.read()'실행 후) : ValueError : 닫힌 파일을 읽습니다. – Wanu

+0

그래서 버전 = 'My new User-Agent'를 Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv : 1.8.1.11) Gecko/20071127 Firefox/2.0.0.11 '으로 변경했습니다. 그리고 그 오류는 사라졌습니다! 매우 큰 감사합니다! 나는이 문제에 대한 해결책을 오랫동안 찾고 있었고, 당신은 나를 많이 도왔습니다! – Wanu

2

아마도 브라우저에서 제공되지 않는다는 사실을 차단하고있을 것입니다. 유효한 User-Agent 헤더 등이 필요할 수도 있습니다.

요청을 사용하여,이 작품 :

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

와우,이 확실히'requests' 라이브러리를 사용하는 모든 사용자에 대한 정답입니다! 내 베이컨을 구해 줬어! – Blairg23