爬取天氣 , 電影 , 貼吧熱議榜等信息定時發送到指定郵箱。
獲取新聞信息:
<code>#!/usr/bin/env python3# -*- coding: utf-8 -*-import socketimport reimport smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport timesock = socket.socket()sock.connect(("paper.people.com.cn", 80))req = "GET /rmrb/html/2019-08/13/nw.D110000renmrb_20190813_2-01.htm HTTP/1.1\\r\\nConnection: close\\r\\nHost: paper.people.com.cn\\r\\n\\r\\n"sock.send(req.encode())data = b''while True: tmp = sock.recv(1024) if tmp: data += tmp else: breaksock.close()data = data.decode(errors="ignore")# print(data)# p1 = data.find("<title>") + len("<title>")# p2 = data.find("/<title>")# news_title = data[p1:p2]# print(news_title)news_title = re.search("<title>(.+?)/<title>", data).group(1)print(news_title)r = re.search("(.+?)", data, flags=re.S)data = r.group(1)data =data.replace(" ", " ")r = re.findall("(.*?)
", data, flags=re.S)news_content = '\\n'.join(r)with open(news_title + ".txt", "w") as f: f.write(news_content) # 第三方 SMTP 服務mail_host="smtp.qq.com" #設置服務器mail_user="[email protected]" #用戶名mail_pass="vcnpthnckxosjebf" #口令 sender = '[email protected]'receivers = ['[email protected]'] # 接收郵件,可設置為你的QQ郵箱或者其他郵箱 message = MIMEText(news_content, 'plain', 'utf-8')message['From'] = Header("dj", 'utf-8')message['To'] = Header("張三", 'utf-8') subject = news_contentmessage['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP_SSL(mail_host) # SMTP over SSL 默認端口號為465 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() print("郵件發送成功")except smtplib.SMTPException as e: print("Error: 無法發送郵件", e)/<title>/<code>
獲取天氣信息:
<code>#!/usr/bin/env python3# -*- coding: utf-8 -*-import socketimport reimport smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport timeimport datetimedef sock(sock_ip, net_path, find_province): sock = socket.socket() sock.connect((sock_ip, 80)) req = ("GET %s HTTP/1.1\\r\\nConnection: close\\r\\nHost: %s \\r\\n\\r\\n" % (net_path, sock_ip)) sock.send(req.encode()) data = b'' while True: tmp = sock.recv(1024) if tmp: data += tmp else: break sock.close() data = data.decode(errors="ignore") return datadef cat(data): for i in data: # print(i) if find_province in i: # print(i) # print(type(i)) i = i.split("\"")[-2] return i# 第三方 SMTP 服務def send_mail(weather_report): mail_host="smtp.qq.com" #設置服務器 mail_user="[email protected]" #用戶名 mail_pass="zgrlczmcuvdnfieg" #口令zgrlczmcuvdnfieg sender = '[email protected]' receivers = ['[email protected]'] # 接收郵件,可設置為你的QQ郵箱或者其他郵箱 message = MIMEText(weather_report, 'plain', 'utf-8') message['From'] = Header("shuijing", 'utf-8') message['To'] = Header("yours", 'utf-8') subject = weather_report message['Subject'] = Header(subject, 'utf-8') while True: try: smtpObj = smtplib.SMTP_SSL(mail_host) # SMTP over SSL 默認端口號為465 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() print("郵件發送成功") break except smtplib.SMTPException as e: print("Error: 無法發送郵件", e)day_time = datetime.date.today() # 獲取當前日期r = str(day_time).split("-", 1)[1].replace("-", '月')sock_ip = "www.nmc.cn"net_path = "/f/rest/province"find_province = "湖北省"data = sock(sock_ip, net_path, find_province)data = re.findall("{.*?}", data)find_city = cat(data)data = sock(sock_ip, find_city, find_province)data = re.findall("<tbody>(.*?)/<tbody>", data, flags=re.S)data = re.sub("<.>", '',data[1]).replace(" ", '')with open('今日天氣.txt', 'wb') as f: f.write(data.encode()) f.closesend_mail(data)/<code>
獲取貼吧信息:
<code>
完整源碼下載地址:https://github.com/crystalworld/life_refer/archive/master.zip
閱讀更多 Python樂園 的文章