如何请求已在python请求中进行URL编码的URL?

时间:2017-03-08 20:14:56

标签: python utf-8 python-requests urllib urlencode

我试图请求以下网址:

https://www.sainsburys.co.uk/shop/gb/groceries/shiraz/barossa-valley-estate-grenache-shiraz-mourv%C3%A8dre-75cl

使用urllib对其进行解码并将其打印显示为:

In [36]: print urllib.unquote(url)
https://www.sainsburys.co.uk/shop/gb/groceries/shiraz/barossa-valley-estate-grenache-shiraz-mourvèdre-75cl

即。一个重音" e"。

但似乎无论我向import requests; requests.get(...)请求什么,我都会得到404.

给get方法提供的正确输入是什么?

1 个答案:

答案 0 :(得分:1)

你应该使用' latin-1'解码网址。将它传递给urrlib unquote之后

>>> 
>>> k = "https://www.sainsburys.co.uk/shop/gb/groceries/shiraz/barossa-valley-estate-grenache-shiraz-mourv%C3%A8dre-75cl"
>>> r = requests.get(urllib.unquote(k).decode("latin-1"))
>>> r.status_code
200
>>>