Python定时向微信发送天气消息和每日一言
Python定时向微信发送天气消息和每日一言
通过wxpy拉取微信好友列表保存至本地Excel 微信自动发送消息至好友,消息内容包括: 天气、http://wufazhuce.com 每日一言。
环境描述
Python 3.7.2 默认安装pip
引用库
#读取excel文件
pip install xlrd
#写excel文件
pip install xlwt
#定时任务框架
pip install apscheduler
#HTML或XML文件中提取数据的Python库
pip install BeautifulSoup
#微信API封装库
pip install wxpy
部分代码
爬取http://wufazhuce.com 每日一言
def oneday():
url='http://wufazhuce.com/one/'#每一期的链接共同的部分
#ran=(datetime.today()-datetime.date()).days+2376
ran=(datetime.date.today()-datetime.date(2019,3,11)).days+2376
currenturl=url+str(ran)#当前期的链接
try:
res=requests.get(currenturl)
res.raise_for_status()
except requests.RequestException as e:#处理异常
print(e)
else:
html=res.text#页面内容
soup = BeautifulSoup(html,'html.parser')
b=soup.select('.one-cita')#查找“每日一句”所在的标签
print(b[0].string.split())
words=str(b[0].string.split())
words = words.replace("['", "[").replace("']", "]")
print(