年后被朋友帶入基金的坑谒兄,天天說(shuō)暴漲,各種牛市社付。于是小白的我就入坑了承疲,稀里糊涂的買(mǎi)了幾個(gè)。結(jié)果還沒(méi)上車(chē)兩天鸥咖,突然暴跌燕鸽,內(nèi)心慘痛。每天在支付寶里面點(diǎn)擊各個(gè)基金看當(dāng)日漲幅感覺(jué)太繁瑣啼辣,于是用python寫(xiě)了個(gè)腳本啊研,這樣方便看下。當(dāng)然還是不看的好鸥拧,這樣你就能忘記傷痛党远。
這邊爬的天天基金的數(shù)據(jù),不過(guò)這個(gè)數(shù)據(jù)和當(dāng)日收益還是會(huì)有點(diǎn)出入富弦。但大體差不多沟娱,除了原油基金!M蠊瘛;ǔ痢(吐嘈易方達(dá)原油)
下面附上代碼
import datetime
import requests
from requests.exceptions import RequestException
from pyquery import PyQuery as pq
def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
}
response = requests.get(url,headers=headers)
if response.status_code == 200:
response.encoding = 'utf-8'
return response.text
return None
except RequestException:
return None
def spider(fund_id):
url = 'http://fund.eastmoney.com/%s.html' % fund_id
html = get_one_page(url)
if html!=None:
doc = pq(html)
name = doc('#body > div:nth-child(12) > div > div > div.fundDetail-header > div.fundDetail-tit > div').text()
value = doc('#gz_gszzl').text()
return name, value
if __name__ == '__main__':
result = {}
fund_id = {'110022','161903'} #####這邊改為你自己的基金代碼
for id in fund_id:
name,value = spider(id)
result[name] = value
now = datetime.datetime.now()
print(now)
result1 = sorted(result.items(),key=lambda x:x[1])
for fund in result1:
print(fund[1]+'\t'+fund[0])
大概結(jié)果將就是這樣,注意是Python3的腳本