1 代码
from requests import get
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
url="http://fund.eastmoney.com/GP_jzzzl.html#os_0;isall_0;ft_;pt_2"
response=get(url,headers=headers)
#print(response.status_code)
response.encoding = 'utf8'
html=response.text
soup=BeautifulSoup(html,"html.parser")
#获取基金代码
listBZDM=[]
a6=soup.find_all("td",class_="bzdm")
for i in range(10):
listBZDM.append(a6[i].getText())
#获取单位净值
listDWJZ1=[]
listDWJZ2=[]
a6=soup.find_all("td",class_="dwjz")
for i in range(0,20,2):
listDWJZ2.append(a6[i].getText())
listDWJZ1.append(a6[i+1].getText())
df=pd.DataFrame()
df["bzdm"]=listBZDM
df["dwjz2"]=listDWJZ2
df["dwjz1"]=listDWJZ1
#转换成数字
df["dwjz2"]=df["dwjz2"].map(lambda x: float(x))
df["dwjz1"]=df["dwjz1"].map(lambda x: float(x))
#计算差值
df["cha"]=df["dwjz2"]-df["dwjz1"]
df.plot()
plt.show()
#print(df)
2 结果
bzdm dwjz2 dwjz1
0 005106 1.9773 1.8988
1 014064 1.9759 1.8975
2 161724 1.7032 1.6446
3 013596 1.7020 1.6435
4 001678 1.2946 1.2539
5 003634 1.9762 1.9143
6 015468 0.9186 0.8899
7 013471 0.9340 0.9082
8 013472 0.9328 0.9070
9 013275 1.7940 1.7450



