rainit2006 / Python-room

my python room
0 stars 0 forks source link

应用例 #8

Open rainit2006 opened 7 years ago

rainit2006 commented 7 years ago
rainit2006 commented 6 years ago

数据分析的应用实例

import re import requests import smtplib from email.mime.text import MIMEText

class Mailer: def init(self, addr_to, subject, body): self.password = '' self.addr_from = 'xxxx@gmail.com' self.addr_to = addr_to self.charset = "ISO-2022-JP" self.subject = subject self.body = body

def send(self):
    msg = MIMEText(self.body.encode(self.charset),'plain', self.charset)
    msg['Subject']=self.subject
    msg['From'] = self.addr_from
    msg['To'] = self.addr_to

    smtp=smtplib.SMTP('smtp.gmail.com', 587)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login(self.addr_from, self.password)
    smtp.send_message(msg)
    smtp.close()

def send_email(value): email_body="""Japanses rate is now at {}""".format(value)

addr_to = 'xxxx'
subject = "japanses rate notification"
mailer = Mailer(addr_to, subject, email_body)
mailer.send()

url = 'http://www.boc.cn/sourcedb/whpj/index.html' # 网址 html = requests.get(url).content.decode('utf8') # 获取网页源码(中间涉及到编码问题,这是个大坑,你得自己摸索)

方式一:正则匹配

a = html.index(r'日元') # 取得“新西兰元”当前位置 s = html[a:a + 300] # 截取日元汇率那部分内容(从a到a+300位置) result = re.findall(r'(.*?)', s) # 正则获取

方式二:lxml获取

result=etree.HTML(html).xpath('//table[@cellpadding="0"]/tr[18]/td/text()')

写入txt

if result[0] is not None: with open(r'汇率.txt', 'w+', encoding='utf8') as f: f.write(result[0] + '\n') f.write(r'现汇买入价:' + result[1] + '\n') f.write(r'现钞买入价:' + result[2] + '\n') f.write(r'现汇卖出价:' + result[3] + '\n') f.write(r'现钞卖出价:' + result[4] + '\n') f.write(r'中行折算价:' + result[5] + '\n') f.write(r'发布时间:' + result[6] + result[7] + '\n')

if float(result[3]) > 6: send_email(result[3])


为了让你的Google账号可以通过程序的方式发送email,需要事先设定“安全性の低いアプリへのアクセスを許可”。
![image](https://user-images.githubusercontent.com/12871721/33940675-40af8694-e053-11e7-9d51-011640880071.png)
rainit2006 commented 6 years ago

圆周率里包含你的银行卡密码吗? https://www.zhihu.com/question/23419402/answer/327661779

搜索圆周率的网页 http://www.angio.net/pi/bigpi.cgi

rainit2006 commented 6 years ago

python调用echart交互式可视化 https://blog.csdn.net/sunchengquan/article/details/79438730