我正在嘗試獲取商品的價格StatTrak? Dual Berettas | Panther (Factory New)。但是 TM 引起了問題,因為 urllib 將其視為非 ascii 字符。我發現了如何使用 urlopen 獲取非 ascii url?但無論出于何種原因,steam 都會傳遞 500 錯誤
from urllib.parse import quote
def get_price(item_name):
base_url = 'http://steamcommunity.com/market/priceoverview/?appid=730¤cy=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¤cy=1&market_hash_name=StatTrak™%20Dual%20Berettas%20|%20Panther%20 New% Factory% 29
作業網址(在瀏覽器中完成):https : //steamcommunity.com/market/priceoverview/?appid=730¤cy=1&market_hash_name= StatTrak™ Dual Berettas | Panther (Factory% 20New)
我似乎需要通過這個,但 urllib 不允許我這樣做。我能做什么?
uj5u.com熱心網友回復:
你的代碼似乎作業正常:
>>> base_url = 'http://steamcommunity.com/market/priceoverview/?appid=730¤cy=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"}'
我相信您的問題是由于雙引號引起的。在%20你的“輸出地址”的意思,你參考一個空格( )兩次(' ' -> -> %20)。
但是,您的代碼似乎只參考一次,這很好。您必須已將已參考的專案傳遞給函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/396485.html
