Python Unicode 请求

Python Request in unicode

我正在尝试制作一个程序,请求 steam 以获得最便宜的商品价格。为此,我将使用 StatTrak™ P250 |以超新星(崭新出厂)为例

问题是在请求的时候,你会做一个url:

http://www.steamcommunity.com/market/priceoverview/?country=SG&currency=13&appid=730&market_hash_name=StatTrak™%20P250%20%7C%20Supernova%20%28Factory%20New%29

之后,(我正在使用请求模块)我这样做:

url = "http://www.steamcommunity.com/market/priceoverview/?country=SG&currency=13&appid=730&market_hash_name=StatTrak™%20P250%20%7C%20Supernova%20%28Factory%20New%29"
requests.get(url)

但是,服务器会return出错。

我似乎找不到替代 ™ 的解决方案。我试过了%2122。在 python 中,我尝试使用 u'\u084a' 但这也不起作用。问题是 python 在请求中按字面意思发送 \u084a 。有什么办法可以解决吗?

只需使用URL编码。您不能在网址中使用 unicode。

>>> import urllib 
>>> f = {'market_hash_name': 'StatTrak™'}
>>> urllib.urlencode(f)
'market_hash_name=StatTrak%E2%84%A2'

也可以

>>> urllib.quote_plus('StatTrak™')