#weather.py
#coding=utf8
import urllib.request
from time import ctime
from bs4 import BeautifulSoup
import itchat
import schedule, time
class Weather():
itchat.auto_login(hotReload=True)
def sel(self):
self.getPM25("地址","备注名")
def get_soup(self,site):
page = urllib.request.urlopen(site)
html = page.read()
soup = BeautifulSoup(html.decode("utf-8"), "html.parser")
return soup
def getPM25(self,cityname,friend):
site = 'http://www.pm25.com/' + cityname + '.html'
soup = self.get_soup(site)
city = soup.find(class_='bi_loaction_city') # 城市名称
aqi = soup.find("a", {"class", "bi_aqiarea_num"}) # AQI指数
quality = soup.select(".bi_aqiarea_right span") # 空气质量等级
result = soup.find("div", class_='bi_aqiarea_bottom') # 空气质量描述
temp = soup.find("p",{"class","bi_info_weather"}) #温度
tempFrom = soup.find("p",{"class","bi_info_tips"}) #数据来源
hr = soup.findAll("a", {"class", "iml_yinshi_right_oneitem_title"})[0]
# soup = self.get_soup(hr["href"])
# content = ''
# question = hr.text
# titles = soup.findAll("strong")
# for title in titles:
# content = content + "\n" + title.text.strip()
# article = question + "\n" + content
output=city.text+ u'\n温度:' + temp.text + u'\nAQI指数:' + aqi.text + u'\n空气质量:' + quality[0].text + result.text + '\n' + tempFrom.text
print(output)
user = itchat.search_friends(friend)
username = user[0]["UserName"]
itchat.send(output,username)
print('*' * 20 + ctime() + '*' * 20)
if __name__ == '__main__':
weather = Weather()
schedule.every().day.at("08:10").do(weather.sel)
#schedule.every(10).minutes.do(weather.sel)
while True:
schedule.run_pending()
time.sleep(1)