马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
此脚本只能在windows系统下正常运行,需要安装bs4、requests、win10toast、keyboard四个外部包。
执行以上代码安装!
脚本会在py文件目录生成一个config.txt文件,里面存储着邮箱地址和密码,属于私人信息。
想要让此脚本发送邮件(目前只限QQ邮箱发送),需要在邮箱账户里设置POP3/SMTP服务,并复制授权码到控制台。
注意,这个py主程序需要调用一个.ico文件,不然Windows10通知的时候会报错,ico文件和py主程序文件需要在同一文件夹下。但是,最好不要将这个文件夹放在桌面,不然有些时候会出bug。
爬取股票价格的时候,会出现超过我能力之外的bug,这个概率大约是1%。
文件夹已上传Github,链接是https://github.com/YouCam-X/Apps
也可以在百度网盘直接下载打包好的程序,链接:https://pan.baidu.com/s/1DHCSobFVCVqw0OlAxPztOw 提取码:hmf4
以下是源代码:
import os
from bs4 import BeautifulSoup
import requests
import time
from urllib.request import quote, unquote
from win10toast import ToastNotifier
import smtplib
from email.mime.text import MIMEText
import keyboard
def edit_data():
global from_addr
global to_addr
global password
global user_agent
from_addr = input('请输入发送邮件的邮箱地址')
to_addr = input('请输入接收邮件的邮箱地址')
password = input('请输入授权码')
user_agent = input('请在浏览器中复制User-Agent参数,贴到这里')
data = [from_addr,to_addr,password,user_agent]
file = open('config.txt','w')
for i in data:
file.write(i)
file.write('\n')
file.close()
def load_previous():
global from_addr
global to_addr
global password
global user_agent
if os.path.isfile('config.txt'):
file = open('config.txt','r')
data = file.readlines()
for i in data:
from_addr = data[0].strip('\n')
to_addr = data[1].strip('\n')
password = data[2].strip('\n')
user_agent = data[3].strip('\n')
file.close()
else:
print('没有已存在的信息,请现在输入')
from_addr = input('请输入发送邮件的邮箱地址')
to_addr = input('请输入接收邮件的邮箱地址')
password = input('请输入授权码')
user_agent = input('请在浏览器中复制User-Agent参数,贴到这里')
data = [from_addr,to_addr,password,user_agent]
file = open('config.txt','w')
for i in data:
file.write(i)
file.write('\n')
file.close()
def sendemail():
message = name + '\n' + str(price.text)
msg = MIMEText(message)
msg['Subject'] = '股票价格预警'
msg['From'] = from_addr
msg['To'] = to_addr
stmp_server = 'smtp.qq.com'
server = smtplib.SMTP_SSL(stmp_server,465,timeout = 2)
server.login(from_addr,password)
server.sendmail(from_addr,[to_addr],msg.as_string())
server.quit()
def main():
global name
global price
loop_number = 0
print(''' 欢迎来到股票通知系统''')
name = input('请输入需要跟踪的股票名称\n')
expectation = input('请输入此股票的预警价格,如>100,<50\n')
number = expectation[1:]
number_without_period = ''
for i in number:
if i == '.':
continue
else:
number_without_period = number_without_period + i
while not str.isdigit(number_without_period):
print('价格输入有误,请重新输入')
expectation = input('请输入预警价格,如>100,<50\n')
number = expectation[1:]
number_without_period = ''
for i in number:
if i == '.':
continue
else:
number_without_period = number_without_period + i
print('是否沿用上一次的邮箱地址和密码?是请按Enter键,重新设置请按Esc键。')
while True:
if keyboard.is_pressed('enter'):
buffer = input('')
load_previous()
break
elif keyboard.is_pressed('esc'):
keyboard.wait('esc',True,True)
edit_data()
break
else:
continue
encodedname = quote(name)
url = 'https://www.baidu.com/s?wd='+ encodedname + '&rsv_spt=1&rsv_iqid=0xc84846ff00069f4b&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=10&rsv_sug1=10&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=4866&rsv_sug4=4866'
headers = {
'referer':'https://www.baidu.com/s?wd='+ encodedname + '&rsv_spt=1&rsv_iqid=0xc84846ff00069f4b&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=10&rsv_sug1=10&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=4866&rsv_sug4=4866',
'user-agent':user_agent}
while True:
toaster = ToastNotifier()
searchpage = requests.get(url,headers=headers)
loop_number += 1
soup = BeautifulSoup(searchpage.text,'html.parser')
price = soup.find(srcid = '5432').find(class_= 'price_2jYb9')
toaster.show_toast(name,price.text,icon_path = 'icon.ico')
print('第%s次请求成功!'%(str(loop_number)))
if expectation[0] == '>':
if float(price.text) >= float(number):
try:
sendemail()
print('通知邮件已发送!')
break
except:
print('发送邮件失败')
break
elif expectation[0] == '<':
if float(price.text) <= float(number):
try:
sendemail()
print('通知邮件已发送!')
break
except:
print('发送邮件失败')
break
time.sleep(60)
main()
press_enter = input('程序已执行完毕,按Enter键退出')
|