import requests import time headers = { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36', 'referer': 'http://fund.eastmoney.com/400015.html?spm=search' } params = { "callback": "jQuery18303118265739643302_1690164521076", "fundCode": "400015", "pageIndex": "6", "pageSize": "20", "startDate": "", "endDate": "", "_": "1690165052233" } url = 'http://api.fund.eastmoney.com/f10/lsjz' res = requests.get(url, params=params, headers=headers) try: data = res.json() LSJZ = data['Data']['LSJZList'] for i in LSJZ: print("单位净值:", i['DWJZ']) except ValueError: print("无效的JSON数据") 输出的是“无效的JSON数据”
时间: 2023-08-14 15:10:00 浏览: 211
【python网络爬虫】python获取聚美优品化妆品价格数据
根据你提供的代码,可能是因为获取到的响应数据不是有效的 JSON 格式导致的。可以尝试打印一下获取到的响应内容,看看是否有其他错误信息。
可以修改代码如下:
```python
import requests
import time
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'referer': 'http://fund.eastmoney.com/400015.html?spm=search'
}
params = {
"callback": "jQuery18303118265739643302_1690164521076",
"fundCode": "400015",
"pageIndex": "6",
"pageSize": "20",
"startDate": "",
"endDate": "",
"_": "1690165052233"
}
url = 'http://api.fund.eastmoney.com/f10/lsjz'
res = requests.get(url, params=params, headers=headers)
try:
data = res.json()
print(data) # 打印获取到的响应内容
LSJZ = data['Data']['LSJZList']
for i in LSJZ:
print("单位净值:", i['DWJZ'])
except ValueError:
print("无效的JSON数据")
```
通过打印获取到的响应内容,可以更好地了解返回的数据结构,并找出问题所在。
阅读全文