data/method/股市/top.py

97 lines
4.5 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 smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
from lxml import etree
import datetime
import re
import time
def timer(n):
while True:
email = MIMEMultipart()
email['From'] = 'sakura_sea_lzk@163.com'
email['To'] = '13479501082@139.com'
email['Subject'] = Header('freesea', 'utf-8')
smtp_obj = smtplib.SMTP_SSL('smtp.163.com', 465)
smtp_obj.login('sakura_sea_lzk@163.com', 'VKHRSRBAQLMJLKPE')
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,3))+ '%'
print('国债收益率: ',c)
r=tree.xpath('/html//body/div//td[@id="ratemv_0"]/text()')
a=float(str(r[0]))*100
c = str(round(a,3))+ '%'
print('股市收益率: ',c)
r=tree.xpath('/html//body/div//td[@id="rategz_0"]/text()')
a=float(str(r[0]))
c = str(round(a,3))
print('股债比: ',c)
c = float(c)
if c>=2.5:
print("投资建议安全股债策略100")
if 2.2<=c<2.5:
print("投资建议较为安全股债策略73")
content = "看财报数据\n"+"股债比:"+str(b)+'\n'+'可以考虑购买'
email.attach(MIMEText(content, 'plain', 'utf-8'))
smtp_obj.sendmail(
'sakura_sea_lzk@163.com',
['13479501082@139.com'],
email.as_string()
)
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[6]//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('//div[@style="color: #e57062"]/text()')
r3 = r2[0].replace("\n","")
print(r3)
rq = re.findall('\W*(\d*\W\d*)',r3,re.A)
a = float(rq[0])/float(r1[1])
b = round(a,2)
print('股债比为:',b)
if b>=3.1:
content = "乐咕网爬取数据计算\n"+"股债比:"+str(b)+'\n'+'可以考虑购买'
email.attach(MIMEText(content, 'plain', 'utf-8'))
smtp_obj.sendmail(
'sakura_sea_lzk@163.com',
['13479501082@139.com'],
email.as_string()
)
print('个人建议:\n3.0对标看财报的2.2,也就是安全区。\n2.5对标2.0\n2.0对标1.5,也就是警戒区')
time.sleep(n)
timer(86400)