python-给微信好友自动发送天气预报和每日一句


周末在宿舍学习python,女朋友那突然下了倾盆大雨,在图书馆门口跟我抱怨好久。最近又在学习python,就想给女朋友写个小程序,每天早上将每天的天气预报通过微信发个她。

在本程序中,用到了几个重要的模块,操作微信的wxpy模块,直接打开网页内容的urlopen,以及搜索html文件的Beautifulsoup在文件开始加上# -*- coding:utf-8 -*-是因为python文件中是不支持中文的,通过开始这个代码可以让文件编码类型改为UTF-8以支持中文。

# -*- coding:utf-8 -*-
import datetime
import time
import wxpy
from urllib.request import urlopen
from bs4 import BeautifulSoup

 通过urlopen模块从想要获取信息的网站获取信息,接着用BeautifulSoup模块解析HTML。再跟据相应的方法取得想要的tag。

 1 #打开中国天气网的绍兴7天天气
 2 resp=urlopen('http://www.weather.com.cn/weather/101210501.shtml')
 3 soup=BeautifulSoup(resp,'html.parser')
 4 
 5 #weather作为明天天气变量
 6 TomorrowWeather=soup.find_all('p',class_="wea")[1].string
 7 TodayWeather=soup.find_all('p',class_="wea")[0].string
 8 
 9 #今天高低温度
10 TodayTemperatureHigh=soup.find_all('p',class_="tem")[0].span.string
11 TodayTemperatureLow=soup.find_all('p',class_="tem")[0].i.string
12 
13 #明天高低温度
14 TomorrowTemperatureHigh=soup.find_all('p',class_="tem")[1].span.string
15 TomorrowTemperatureLow=soup.find_all('p',class_="tem")[1].i.string

接着用类似的方法从“ONE”上获取每日一句。

1 """获取每日一句的内容"""
2 resp=urlopen('http://www.wufazhuce.com/')
3 soup=BeautifulSoup(resp,'html.parser')
4 
5 text=soup.find_all('a')[2].string

最后通过模块datetime获取时间,并设定好时间发送这些消息。

 1 '''get time now'''
 2 nowtime=datetime.datetime.now()
 4 '''send message at time'''
 5 if nowtime.hour==7 and nowtime.minute==0:
 6     print('send weather forecast')
 7     weather=get_weather()
 8     girlfriend=bot.search('Blueberry')[0]
 9     girlfriend.send(weather)
10     if TodayWeather.find('')!=-1 :
11         girlfriend.send('出门记得带好伞哦~')
12     time.sleep(60)
13 if nowtime.hour==22 and nowtime.minute==0:
14     print('send news')
15     dailysentence=news.get_news()
16     girlfriend=bot.search('Blueberry')[0]
17     girlfriend.send(dailysentence)
18     girlfriend.send('--每日一句')
19     time.sleep(60)

以上就是全部的代码了。这是我学习了python后第一次自己编写的一个完整的代码,很简单。就当练练手,以后如果工作有自动化测试的需要,再尝试写点其他的。

学以致用。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM