Skip to content

Commit 05b1c8b

Browse files
committed
submit code
1 parent 0fb19e2 commit 05b1c8b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

chaoxi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
+ [Matplotlib_3D](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Matplotlib_3D) :Python 30 行代码画各种 3D 图形
1010
+ [five_code](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/five_code) :Python 5 行代码的神奇操作!
1111
+ [python_sort](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/python_sort) :Python 排序了解一下?
12+
+ [send_weather](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/send_weather) :Python 小技能之抓取天气信息发送给小姐姐
1213

1314
---
1415

chaoxi/send_weather/send_weather.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
import smtplib
3+
from email.mime.text import MIMEText
4+
from email.header import Header
5+
import requests
6+
from bs4 import BeautifulSoup
7+
import prettytable as pt
8+
9+
def get_Data(url):
10+
data_list = []
11+
response = requests.get(url)
12+
html_doc = response.text
13+
soup = BeautifulSoup(html_doc, 'lxml') # 自动补全html代码,并按html代码格式返回
14+
wendu = soup.find('div', class_='temperature').get_text()
15+
tianqi = soup.find('div', class_='weather-icon-wrap').get_text()
16+
data_list.append("现在的温度:%s\n现在天气情况:%s" % (wendu, tianqi))
17+
list = soup.find_all('ul', class_='weather-columns')
18+
for item in list:
19+
data_list.append(item.get_text())
20+
print("列表数据:",data_list)
21+
a = 1
22+
tb = pt.PrettyTable() #创建PrettyTable对象
23+
tb.field_names = ["日期","天气","详情"]
24+
for item in data_list:
25+
# print(a)
26+
if a != 1:
27+
tb.add_row([item.strip().split()[0]+item.strip().split()[1],item.strip().split()[2],item.strip().split()[3]])
28+
else: print(item.strip())
29+
a+=1
30+
print(tb)
31+
return tb
32+
33+
34+
35+
def send_mail(msg,receiver):
36+
# 收件人
37+
receiver = receiver
38+
mail_title = '小姐姐,请查收今天以及往后15天的天气预报,愿你三冬暖,春不寒'
39+
mail_body = str(msg)
40+
# 创建一个实例
41+
message = MIMEText(mail_body, 'plain', 'utf-8') # 邮件正文
42+
# (plain表示mail_body的内容直接显示,也可以用text,则mail_body的内容在正文中以文本的形式显示,需要下载)
43+
message['From'] = sender # 邮件的发件人
44+
message['To'] = receiver # 邮件的收件人
45+
message['Subject'] = Header(mail_title, 'utf-8') # 邮件主题
46+
47+
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465) # 创建发送邮件连接
48+
smtp.connect(smtpserver) # 连接发送邮件的服务器
49+
smtp.login(username, password) # 登录服务器
50+
smtp.sendmail(sender, receiver, message.as_string()) # 填入邮件的相关信息并发送
51+
52+
smtp.quit()
53+
54+
55+
56+
if __name__ == '__main__':
57+
sender = 'xxx@qq.com'
58+
# 发件人邮箱的SMTP服务器(即sender的SMTP服务器)
59+
smtpserver = 'smtp.qq.com'
60+
# 发件人邮箱的用户名和授权码(不是登陆邮箱的密码)
61+
username = 'xxxxxx'
62+
# 邮箱授权码
63+
password = 'xxxxx'
64+
url1 = 'https://tianqi.so.com/weather/'
65+
url_list = ['url1','url1']
66+
receiver_list ='xxx@qq.com'
67+
tb = get_Data(url1) #获得每一个用户的数据
68+
send_mail(tb,receiver_list) #发送邮件
69+
#

0 commit comments

Comments
 (0)