1. 程式人生 > >微信定時發送天氣python3

微信定時發送天氣python3

url www urllib requests 今天 result ups find 包含

爬取天氣信息,定時發送天氣給多人python3

 1 import requests
 2 from requests import exceptions
 3 from urllib.request import urlopen
 4 from bs4 import BeautifulSoup
 5 import re
 6 from wxpy import *
 7 import  schedule
 8 import  time
 9  
10  
11 #bot=Bot(cache_path=True) #登陸網頁微信,並保存登陸狀態
12 bot = Bot(console_qr=2,cache_path="
botoo.pkl")#Linux專用,像素二維碼 13 14 def sendblogmsg(content): 15 #搜索自己的好友,註意中文字符前需要+u 16 my_friend = bot.friends().search(u王琳傑)[0] 17 my_friend.send(content) 18 19 my1_friend = bot.friends().search(u浮生若夢)[0] 20 my1_friend.send(content) 21 22 my_group = bot.groups().search(u
聊天機器人測試)[0] 23 my_group.send(content) #發送天氣預報 24 25 26 27 my1_group = bot.groups().search(u測試)[0] 28 my1_group.send(content) #發送天氣預報 29 30 def job(): 31 resp=urlopen(http://www.weather.com.cn/weather/101010100.shtml) 32 soup=BeautifulSoup(resp,html.parser) 33 tagToday=soup.find(
p,class_="tem") #第一個包含class="tem"的p標簽即為存放今天天氣數據的標簽 34 try: 35 temperatureHigh=tagToday.span.string #有時候這個最高溫度是不顯示的,此時利用第二天的最高溫度代替。 36 except AttributeError as e: 37 temperatureHigh=tagToday.find_next(p,class_="tem").span.string #獲取第二天的最高溫度代替 38 39 temperatureLow=tagToday.i.string #獲取最低溫度 40 weather=soup.find(p,class_="wea").string #獲取天氣 41 contents = 北京 + \n + 最高溫度: + temperatureHigh + \n + 最低溫度: + temperatureLow + \n + 天氣: + weather 42 # result3 = ‘最低溫度:‘ + temperatureLow 43 #print(‘最低溫度:‘ + temperatureLow) 44 #print(‘最高溫度:‘ + temperatureHigh) 45 # print(‘天氣:‘ + weather) 46 sendblogmsg(contents) 47 #定時 48 schedule.every().day.at("22:45").do(job) #規定每天12:30執行job()函數 49 while True: 50 schedule.run_pending()#確保schedule一直運行 51 time.sleep(1) 52 bot.join() #保證上述代碼持續運行

微信定時發送天氣python3