需求:
**:[root@pos ~]# cat weather.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests,bs4,smtplib
def sendmail(body):
smtp_server = 'smtp.126.com'
from_mail = '[email protected]'
mail_pass = '*********x'
to_mail = ['50******[email protected]']
from_name = 'weather monitor'
subject = 'raining today!'
mail = [
"from: %s " % (from_name, from_mail),
"to: %s" % ','.join(to_mail),
"subject: %s" % subject,
body
msg = '\n'.join(mail)
try:
s = smtplib.smtp_ssl('smtp.126.com',465)
s.login(from_mail, mail_pass)
s.sendmail(from_mail, to_mail, msg)
s.quit()
except smtplib.smtpexception as e:
print("error: "+e)
if __name__ == "__main__":
headers=
url=''
urlhtml=requests.get(url,headers=headers)
urlhtml.raise_for_status()
weatherhtml=bs4.beautifulsoup(urlhtml.text[:],'html.parser')
weather=weatherhtml.select('div li p')
for i in range(len(weather)):
weathnow=weather[i].gettext()
if weathnow == '有雨':
sendmail("it's rainy today. remember to bring your umbrella!")
print("done!")
將指令碼寫入計畫任務,每天6點執行一次。
計畫任務可以採用task scheduler(windows)、launchd(os x)、cron(linux)等系統工具。
當然,也可以採用傳送簡訊的方式進行通知。
python天氣查詢 python天氣查詢
主要通過字典查詢全國各地主要城市天氣情況 from urllib.request import urlopen from bs4 import beautifulsoup import re citylist main 全國主要城市 北上廣深,華北,東北,華南,西北,西南,華東,華中,def cit...
利用google天氣api獲取最近的天氣
weather dim url,xml,xmldom,oitem,link,title,dateandtime,ncontent url ig api?hl zh cn weather seoul microsoft.xmlhttp物件,能夠完成從資料報到request物件的轉換以及傳送任務 利用m...
Python天氣爬蟲
建立beautifulsoup物件 body bs.body 獲取body部分 data body.find div 找到id為7d的div ul data.find ul 獲取ul部分 li ul.find all li 獲取所有的li for day in li 對每個li標籤中的內容進行遍歷 ...