简体   繁体   English

如何修复python请求错误“期望值”

[英]how to fix python request error "Expecting value"

i'm trying to get the json response of couple of product ids page.我正在尝试获取几个产品 ID 页面的 json 响应。 I used this我用过这个

Python : how to set parameters for python request Python:如何为python请求设置参数

to succesfully query a word response but I don't know why query by product id is not working.成功查询单词响应,但我不知道为什么按产品 ID 查询不起作用。 I started with with this我从这个开始

this is actual url to the product这是产品的实际网址

https://www.walmart.com/ip/Breyers-CarbSmart-Frozen-Dairy-Dessert-Chocolate-48-oz/10898747 https://www.walmart.com/ip/Breyers-CarbSmart-Frozen-Dairy-Dessert-Chocolate-48-oz/10898747

so I started with this code所以我从这段代码开始

prodID = '10898747'


url = 'https://www.walmart.com/ip/'

params = {
    
    'product_id': prodID,

}

data = requests.get(url, params=params).json()
print(data)

but it's produces this error但它产生了这个错误

Expecting value: line 1 column 1 (char 0)期望值:第 1 行第 1 列(字符 0)

what am I doing wrong and how can I fix this?我做错了什么,我该如何解决?

The url is not returning a valid json object. url 未返回有效的 json 对象。

Going there in my browser yields a 404: https://www.walmart.com/ip/?product_id=743876032在我的浏览器中转到那里会产生 404: https : //www.walmart.com/ip/? product_id =743876032

And running it from a fiddle, it returns some sort of forbidden string https://repl.it/repls/BossyHoneydewMicrostation并从小提琴运行它,它返回某种禁止的字符串https://repl.it/repls/BossyHoneydewMicrostation

In other words, the problem is not in your code but the request is not working.换句话说,问题不在于您的代码,而是请求不起作用。

To prevent such errors, check the response: How to determine if my Python Requests call to API returns no data为防止此类错误,请检查响应: 如何确定我对 API 的 Python 请求调用是否不返回数据

try:
    response = response.json
except ValueError:
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM