from __future__ import unicode_literals from threading import Timer from wxpy import * from urllib.request import urlopen from bs4 import BeautifulSoup import requests import schedule import time import urllib import json import re
bot = Bot(cache_path=True)
def job(): resp = urlopen('http://www.weather.com.cn/weather/101190101.shtml') soup = BeautifulSoup(resp, 'html.parser') tagToday = soup.find('p', class_="tem") try: temperatureHigh = tagToday.span.string except AttributeError as e: temperatureHigh = tagToday.find_next('p', class_="tem").span.string
temperatureLow = tagToday.i.string weather = soup.find('p', class_="wea").string contents = '南京' + '\n' + '最高温度:' + temperatureHigh + '\n' + '最低温度:' + temperatureLow + '\n' + '天气:' + weather return contents
def get_news(): url = "http://open.iciba.com/dsapi/" r = requests.get(url) content = r.json()['content'] note = r.json()['note'] weather_info = job() return content, note, weather_info
def send_news(): try: content, note, weather = get_news() my_friend = bot.friends().search(u'')[0] my_friend.send(weather) my_friend.send("this is a python test! Morning!") """ schedule.every().day.at("12:30").do(job) # 规定每天12:30执行job()函数 while True: schedule.run_pending()#确保schedule一直运行 time.sleep(1) """ t = Timer(60, send_news) t.start()
except: my_friend = bot.friends().search('帆')[0] my_friend.send(u'send error')
if __name__ == "__main__": send_news() bot.join()
|