Python-爬取中国天气网天气并通过邮箱定时发送
生活随笔
收集整理的這篇文章主要介紹了
Python-爬取中国天气网天气并通过邮箱定时发送
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取天氣信息腳本如下,
#!/usr/bin/python3 # -*- coding: utf-8 -*- import re import requests from bs4 import BeautifulSoup import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')r = requests.get('http://www.weather.com.cn/weather/101120301.shtml',timeout = 30) r.raise_for_status() r.encoding = 'utf-8'rdata = re.findall(r'<h1>.*?</h1>',r.text) rwea = re.findall(r'\"wea\">.*?</p>',r.text) rtemp1 = re.findall(r'\/<i>.*?</i>',r.text) rtemp2 = re.findall(r'<span>\d+\.?\d*</span>',r.text) for i in range(6):data = rdata[i].split('>')[1].split('<')[0]wea = rwea[i].split('>')[1].split('<')[0]temp1 = rtemp1[i].split('>')[1].split('<')[0]temp2 = rtemp2[i].split('>')[1].split('<')[0]# temp2 = rtemp2[i].split('>')[1].split('<')[0]# tplt = "{0:^10}\t{1:{6}^10}\t{2:}\t{3:<}\t{4:}\t{5:}" tplt = "{0:^10}\t{1:{4}^10}\t{2:}\t{3:<}\t{4:}" print(tplt.format(data,wea,temp1,"~" + temp2 ,chr(12288)))保存為crawlChineseWeather.py,將其上傳服務(wù)器,然后安裝腳本所需要的庫
sudo apt install python3-pip sudo apt-get install python3-bs4然后在服務(wù)器中安裝mail服務(wù)
Centos7默認安裝了mail
Ubuntu16.04運行如下命令安裝
添加一個定時任務(wù),每天7:00am發(fā)送天氣信息到給定郵箱
0 7 * * * python3 crawlChineseWeather.py | mail -s subject email-Address完成
總結(jié)
以上是生活随笔為你收集整理的Python-爬取中国天气网天气并通过邮箱定时发送的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: deepin--解决网易云音乐个性推荐无
- 下一篇: Python利用itchat库向好友或者