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

43 lines
1.4 KiB
Python
Raw Normal View History

2024-01-29 10:44:43 +08:00
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("投资建议:赶紧跑")