Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 再次urllib.error.HTTPError:HTTP错误400:Bad请求_Python_Python 3.x_Urllib_Http Status Code 400_Urlopen - Fatal编程技术网

Python 再次urllib.error.HTTPError:HTTP错误400:Bad请求

Python 再次urllib.error.HTTPError:HTTP错误400:Bad请求,python,python-3.x,urllib,http-status-code-400,urlopen,Python,Python 3.x,Urllib,Http Status Code 400,Urlopen,嘿! 我试图打开通常在浏览器中打开的网页,但python只是发誓不想工作 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

嘿! 我试图打开通常在浏览器中打开的网页,但python只是发誓不想工作

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行,在
文件“C:\Python34\lib\urllib\request.py”,第461行,打开
响应=方法(请求,响应)
文件“C:\Python34\lib\urllib\request.py”,第571行,在http\U响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python34\lib\urllib\request.py”第493行出错
结果=自身调用链(*args)
文件“C:\Python34\lib\urllib\request.py”,第433行,在调用链中
结果=func(*args)
文件“C:\Python34\lib\urllib\request.py”,第676行,在http\u error\u 302中
返回self.parent.open(新建,超时=请求超时)
文件“C:\Python34\lib\urllib\request.py”,第461行,打开
响应=方法(请求,响应)
文件“C:\Python34\lib\urllib\request.py”,第571行,在http\U响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python34\lib\urllib\request.py”第499行出错
返回自我。调用链(*args)
文件“C:\Python34\lib\urllib\request.py”,第433行,在调用链中
结果=func(*args)
文件“C:\Python34\lib\urllib\request.py”,第579行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误400:请求错误

有什么想法吗?

他们可能正在阻止这一事实,即它不是来自浏览器。您可能需要一个有效的用户代理头或其他东西

通过使用请求,可以实现以下功能:

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

此URL似乎正在执行用户代理字符串检查。如果我在Firefox中将我的用户代理字符串调整为
pythonURLLIB/2.7
,它将失败,出现您看到的
错误请求

在使用
urllib
时,可以按照以下步骤调整用户代理


谢谢,只是我将“从urllib导入FancyURLopener”更改为“从urllib.request导入FancyURLopener”(was错误)。最后我遇到了下一个错误(在运行'>>>page.read()'之后):ValueError:read of closed file.Wow,对于使用
请求
库的任何人来说,这绝对是正确的答案!救了我的命!
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')