@大山深处
试试这个:
import requests #爬基金的VALUE和IOPV
from bs4 import BeautifulSoup
def get_etf_value(stockcode):
# 取基金代码的前6位
stockcode = stockcode[:6]
url = "https://fundf10.eastmoney.com/jjjz_" + stockcode + ".html"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
# 找到基金净值的表格,其class为"w782 comm lsjz"
target_table = soup.find('table', class_="w782 comm lsjz")
if not target_table:
return -1
else:
# 找到table中的所有行
rows = target_table.find_all('tr')
# 除去表头,基金净值在第一行的第2列(索引值为1)
etf_value_td = rows[1].find_all('td')[1]
etf_value = float(etf_value_td.get_text().strip())
print('etf_value', etf_value)
return etf_value
2023-12-23