Skip to main content
 首页 » 编程设计

python之如何使用 json 文件使用 url lib

2024年12月31日8think

我正在尝试从 json 链接获取数据,但出现此错误:TypeError: can't concat str to bytes

这是我的代码:

l = "https://www.off---white.com/en/IT/men/products/omch016f18d471431088s" 
url = (l+".json"+"?porcoiddio") 
req = urllib.request.Request(url, headers) 
response = urllib.request.urlopen(req) 
 
size_opts = json.loads(response.decode('utf-8'))['available_sizes'] 

我该如何解决这个错误?

请您参考如下方法:

您的问题答案是将您的代码更改为:

size_opts = json.loads(response.read().decode('utf-8'))['available_sizes'] 

2018-10-02 22:55 更改:我查看你的源代码,发现 Response 503 ,你得到503的原因是请求没有包含cookie:

req = urllib.request.Request(url, headers=headers) 

您已经更新了标题。

headers.update({"Cookie":cookie_value}) 
req = urllib.request.Request(url, headers=headers) # !!!! you need a headers include cookies !!!!