[英]urllib2 HTTP Error 400: Bad Request
I have a piece of code like this 我有一段这样的代码
host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (query, page)
req = urllib2.Request(host)
req.add_header('User-Agent', User_Agent)
response = urllib2.urlopen(req)
and when I input a query greater than one word like "the dog" i get the following error. 当我输入的查询超过一个单词,例如“狗”时,出现以下错误。
response = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Can anyone point out what im doing wrong? 谁能指出我做错了什么? Thanks in advance. 提前致谢。
The reason that "the dog" returns a 400 Error is because you aren't escaping the string for a URL. “狗”返回400错误的原因是因为您没有转义URL的字符串。
If you do this: 如果您这样做:
import urllib, urllib2
quoted_query = urllib.quote(query)
host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (quoted_query, page)
req = urllib2.Request(host)
req.add_header('User-Agent', User_Agent)
response = urllib2.urlopen(req)
It will work. 它会工作。
However I highly suggest you use requests instead of using urllib/urllib2/httplib. 但是,我强烈建议您使用请求,而不要使用urllib / urllib2 / httplib。 It's much much easier and it'll handle all of this for you. 这要容易得多,它将为您处理所有这一切。
This is the same code with python requests: 这是与python请求相同的代码:
import requests
results = requests.get("http://www.bing.com/search",
params={'q': query, 'first': page},
headers={'User-Agent': user_agent})
You need to use urllib.quote()
on your 'query' variable: 您需要在“查询”变量上使用urllib.quote()
:
query = urllib.quote(query)
host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (query, page)
This does the necessary URL escaping to convert the space in big dog
to big%20dog
. 这会进行必要的URL转义,以将big dog
的空间转换为big%20dog
。
您必须使用urllib.quote
Here is an example of how to use urllib.request object in Python 3.6 and above. 这是在Python 3.6及更高版本中如何使用urllib.request对象的示例。
import urllib.request
import json
from pprint import pprint
url = "some_url"
values = {
"first_name": "Vlad",
"last_name": "Bezden",
"urls": [
"https://twitter.com/VladBezden",
"https://github.com/vlad-bezden",
],
}
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
data = json.dumps(values).encode("utf-8")
pprint(data)
try:
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as f:
res = f.read()
pprint(res.decode())
except Exception as e:
pprint(e)
I also encountered the same problem. 我也遇到了同样的问题。 Turns out the problem was the method was set inappropriately. 原来的问题是方法设置不当。 When you include urlencoded data in urllib2.urlopen () the method should be set to POST and when you exclude it, method should be GET. 当在urllib2.urlopen()中包含urlencoded数据时,该方法应设置为POST,而在排除它时,方法应为GET。 So, how do you set the method is given below: 因此,如何设置方法如下:
For POST request 对于POST请求
request_object = urllib2.Request(url)
method = ("POST", "GET")
request_object.get_method = lambda: method[0] #If method is set to POST
url_handle = opener.open(req, data) #If method is set to POST
For GET request 对于GET请求
request_object = urllib2.Request(url)
method = ("POST", "GET")
request_object.get_method = lambda: method[1] #If method is set to GET
url_handle = opener.open(req) #If method is set to GET
This will set your url request method to the appropriate required method 这会将您的url请求方法设置为适当的必需方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.