data/method/python/例程/bonus/top.py

64 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
from lxml import etree
import datetime
ISOTIMEFORMAT = '%Y-%m-%d %H:%M:%S'
theTime = datetime.datetime.now().strftime(ISOTIMEFORMAT)
print('当前时间:',theTime)
url = 'https://www.kancaibao.com/ep'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36 Edg/103.0.1264.44'
}
respons = requests.get(url =url,headers = headers)
tree = etree.HTML(respons.text)
r=tree.xpath('/html//body/div//td[@id="date_0"]/text()')
print('获取的数据如下\n时间:',r[0])
r=tree.xpath('/html//body/div//td[@id="gzclose_0"]/text()')
a=float(str(r[0]))*100
c = str(round(a,2))+ '%'
print('国债收益率: ',c)
r=tree.xpath('/html//body/div//td[@id="ratemv_0"]/text()')
a=float(str(r[0]))*100
c = str(round(a,2))+ '%'
print('股市收益率: ',c)
r=tree.xpath('/html//body/div//td[@id="rategz_0"]/text()')
a=float(str(r[0]))
c = str(round(a,2))
print('股债比: ',c)
c = float(c)
if c>=2.5:
print("投资建议安全股债策略100")
if 2.2<=c<2.5:
print("投资建议较为安全股债策略73")
if 2<=c<2.2:
print("投资建议谨慎乐观股债策略64")
if 1.8<=c<2:
print("投资建议普通股债策略55")
if 1.55<=c<1.8:
print("投资建议有风险股债策略46")
if c<1.55:
print("投资建议:赶紧跑")
print('\n从乐咕网上获取的数据如下')
url1 = "https://legulegu.com/stockdata/hs300-ttm-lyr"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
res = requests.get(url = url1,headers = headers)
tree1 = etree.HTML(res.text)
r1 = tree1.xpath('/html//div[@class="data-description"]//tr[3]//td/text()')
print(r1[0],':',r1[1])
url2 = 'https://legulegu.com/stockdata/china-10-year-bond-yield'
res = requests.get(url = url2,headers = headers)
tree2 = etree.HTML(res.text)
r2 = tree2.xpath('/html//div[@class="market-data"]//span[2]/text()')
r3 = r2[0].replace('\n','')
r4 = r3.replace('\t','')
print(r4)
import re
rq = re.findall('\W*(\d*\W\d*)',r4,re.A)
a = float(rq[0])/float(r1[1])
b = round(a,2)
print('股债比为:',b)
print('个人建议3.1对标看财报的2.2,也就是安全区')