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 = { "scheme": "http", "host": "api.fund.eastmoney.com", "path": "/f10/lsjz", "query": { "callback": "jQuery18303118265739643302_1690164521076", "fundCode": "400015", "pageIndex": "6", "pageSize": "20", "startDate": "", "endDate": "", "_": "1690165052233" } } res = requests.get( 'http://fundf10.eastmoney.com/jjjz_400015.html.api', params=params, headers=headers) LSJZ = res.json()['Data']['LSJZList'] for i in LSJZ: print("单位净值:",i['DWJZ']) 这段代码运行不起来,修改一下使其正常运行
时间: 2023-08-12 13:05:52 浏览: 140
【python网络爬虫】python获取聚美优品化妆品价格数据
这段代码无法正常运行的原因是,params参数被错误地添加到了请求的URL中。正确的做法是将params作为requests.get()函数的参数传递,而不是将其作为URL的一部分。
以下是修正后的代码:
```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)
LSJZ = res.json()['Data']['LSJZList']
for i in LSJZ:
print("单位净值:", i['DWJZ'])
```
这样修改后的代码会将params参数正确地传递给requests.get()函数,并且使用正确的URL进行请求。然后可以成功获取数据并打印出单位净值。
阅读全文