urllib2 HTTP 错误 400:错误请求 [英] urllib2 HTTP Error 400: Bad Request

查看:110
本文介绍了urllib2 HTTP 错误 400:错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段这样的代码

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 dog"返回 400 错误的原因是因为您没有转义 URL 的字符串.

The reason that "the dog" returns a 400 Error is because you aren't escaping the string for a URL.

如果你这样做:

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)

它会起作用.

但是我强烈建议您使用 requests 而不是使用 urllib/urllib2/httplib.这要容易得多,而且它会为您处理所有这些.

However I highly suggest you use requests instead of using urllib/urllib2/httplib. It's much much easier and it'll handle all of this for you.

这是与python请求相同的代码:

This is the same code with python requests:

import requests

results = requests.get("http://www.bing.com/search", 
              params={'q': query, 'first': page}, 
              headers={'User-Agent': user_agent})

这篇关于urllib2 HTTP 错误 400:错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆