python爬取510180基金历史净值
时间: 2023-10-22 21:04:40 浏览: 125
可以使用Python的requests和BeautifulSoup库来爬取基金的历史净值数据。以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
# 基金历史净值页面的URL
url = 'http://fundf10.eastmoney.com/jjjz_510180.html'
# 发送HTTP GET请求
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取历史净值表格
table = soup.find('table', class_='w782 comm lsjz')
# 获取所有行
rows = table.find_all('tr')
# 遍历每一行,输出日期和净值
for row in rows[1:]:
cols = row.find_all('td')
date = cols[0].text.strip()
value = cols[1].text.strip()
print(date, value)
```
阅读全文