Python 如何请求已引用的URL?

Python 如何请求已引用的URL?,python,python-requests,Python,Python Requests,前面的代码为我提供了以下url:。现在,我想申请,但想不出办法: >>> requests.get('http://en.wikipedia.org/wiki/M%C3%BCnster') <Response [400]> >>> requests.get(urlparse.unquote('http://en.wikipedia.org/wiki/M%C3%BCnster')) <Response [400]> >>>

前面的代码为我提供了以下url:。现在,我想申请,但想不出办法:

>>> requests.get('http://en.wikipedia.org/wiki/M%C3%BCnster')
<Response [400]>
>>> requests.get(urlparse.unquote('http://en.wikipedia.org/wiki/M%C3%BCnster'))
<Response [400]>
>>> requests.get(urlparse.unquote('http://en.wikipedia.org/wiki/M%C3%BCnster').decode('utf-8'))
<Response [400]>
有什么想法吗?

尝试添加一个
.decode('utf-8')


带有自定义用户代理头的简单urlparse.unquote似乎可以完成这项工作

>>> s = 'http://en.wikipedia.org/wiki/M%C3%BCnster'
>>> import urllib2, urlparse
>>> headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'}
>>> url = urlparse.unquote(s)
>>> req = urllib2.Request(url, None, headers)
>>> resp = urllib2.urlopen(req)
>>> print resp.code
200
>>> data = resp.read()
>>> print 'The last outstanding palace of the German baroque period is created according to plans by Johann Conrad Schlaun.' in data
True

不要将字节字符串解码为unicode对象,这会导致UnicodeEncodeError:“ascii”编解码器无法对urlopen中位置11:ordinal不在范围(128)中的字符u'\xfc'进行编码。

这是请求中的错误。它已在
develope
分支中修复。请参阅:。

不,不适合我。将其添加到上面的描述中。不适用于urllib或urllib2,但给出了错误403…似乎urllib*的问题是因为Wikipedia服务器不喜欢它,与请求问题无关。@maciej看到它是一个python模块,称自己为“HTTP for Humans”。我觉得很不错。但我希望通过请求找到解决方案。这是一个不同于urllib的库,非常好,但是如果我不能用它解决这个问题,我需要恢复到urllib:/
requests.get(urlparse.unquote('http://en.wikipedia.org/wiki/M%C3%BCnster').decode('utf-8'))
>>> s = 'http://en.wikipedia.org/wiki/M%C3%BCnster'
>>> import urllib2, urlparse
>>> headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'}
>>> url = urlparse.unquote(s)
>>> req = urllib2.Request(url, None, headers)
>>> resp = urllib2.urlopen(req)
>>> print resp.code
200
>>> data = resp.read()
>>> print 'The last outstanding palace of the German baroque period is created according to plans by Johann Conrad Schlaun.' in data
True