【问题标题】:Passing a non ascii character in steam url在 Steam url 中传递非 ascii 字符
【发布时间】:2021-12-27 16:44:22
【问题描述】:

我正在尝试获取商品StatTrak™ Dual Berettas | Panther (Factory New) 的价格。但是 TM 会导致问题,因为 urllib 将其视为非 ascii 字符。我找到了这个How to fetch a non-ascii url with urlopen?,但无论出于何种原因,steam 都出现了 500 错误

from urllib.parse import quote 

def get_price(item_name):
    base_url = 'http://steamcommunity.com/market/priceoverview/?appid=730&currency=1&market_hash_name={}'
    print(base_url.format(quote(item_name)))
    request = urllib.request.urlopen(base_url.format(quote(item_name)))

输出 URL(解析后):http://steamcommunity.com/market/priceoverview/?appid=730&currency=1&market_hash_name=StatTrak%E2%84%A2%2520Dual%2520Berettas%2520%7C%2520Panther%2520%28Factory%2520New%29

工作网址(在浏览器中完成):https://steamcommunity.com/market/priceoverview/?appid=730&currency=1&market_hash_name=StatTrak%E2%84%A2%20Dual%20Berettas%20|%20Panther%20(Factory%20New)

我似乎需要通过这个,但 urllib 不允许我通过。我能做什么?

【问题讨论】:

    标签: python url encoding urllib


    【解决方案1】:

    您的代码似乎运行良好:

    >>> base_url = 'http://steamcommunity.com/market/priceoverview/?appid=730&currency=1&market_hash_name={}'
    >>> urllib.request.urlopen(base_url.format(quote("StatTrak™ Dual Berettas | Panther (Factory New)"))).read()
    b'{"success":true,"lowest_price":"$5.80","volume":"3","median_price":"$4.53"}'
    

    我相信您的问题是由于双引号引起的。 “输出 URL”中的 %2520 表示您引用了一个空格 (%20) 两次 (' ' -> %20 -> %2520)。

    但是,您的代码似乎只引用了一次,这很好。您必须已将已引用的项目传递给函数。

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 2013-06-19
      • 2023-03-22
      • 2020-12-21
      • 2021-12-31
      • 2021-07-28
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多