标签:name now() strftime tin form cas 启动 sea 函数
1 from wxpy import * 2 3 4 def login(): 5 bot = Bot(cache_path=True) 6 my_lover = bot.friends().search(‘夏叶‘)[0] 7 return my_lover
search方法接收一个昵称的字符串,它会返回一个查找到的所有条件的列表对象,我们这里只有这一个人,直接选第一个对象
1 import requests 2 from lxml import etree 3 4 5 def get_page(url): 6 r = requests.get(url) 7 r.encoding = r.apparent_encoding 8 return r.text if r.status_code == 200 else None 9 10 11 def parse_page(html): 12 html = etree.HTML(html) 13 forecasts = html.xpath(‘/html/body/div[8]/div[1]/div[1]/div[2]/ul/li/a[1]/@title‘) 14 forecasts = ‘\n‘.join(forecasts) 15 keys = html.xpath(‘/html/body/div[8]/div[2]/div[6]/ul/li/b/text()‘) 16 values = html.xpath(‘/html/body/div[8]/div[2]/div[6]/ul/li/a/p/text()‘) 17 day_info = {i: values[keys.index(i)] for i in keys} 18 message = ‘青哥哥今日提醒:\n\n‘ + ‘南京今日生活指数:\n‘ + ‘\n‘.join( 19 [‘{}: {}‘.format(i, day_info[i]) for i in day_info]) + ‘\n‘ * 3 + ‘南京主要地区天气预报:\n‘ + forecasts 20 return message
爬虫库使用的是requests,解析库用的是xpath,最后将字符串拼接,返回消息对象
1 from datetime import datetime 2 3 4 def get_time(): 5 time = datetime.now().strftime(‘%H:%M:%S‘) 6 return time 7 8 9 clock = ‘06:00:0{}‘ # 设置启动时间 10 interval = 3 # 设置时间间隔 11 time_zone = [clock.format(i) for i in range(interval)]
防止电脑性能过差或cpu使用率过高导致的时间漏缺,设置一下时间间隔,我这里设置的是三秒,最后将设置时间区间
1 def main(my_lover): 2 url = ‘http://www.tianqi.com/nanjing/‘ 3 html = get_page(url) 4 message = parse_page(html) 5 my_lover.send(message)
不同城市的url可以去天气网自定义,一般网页的结构是不会变得
1 from time import sleep 2 from datetime import datetime 3 from wxpy import * 4 import requests 5 from lxml import etree 6 7 8 def login(): 9 bot = Bot(cache_path=True) 10 my_lover = bot.friends().search(‘夏叶‘)[0] 11 return my_lover 12 13 14 def get_page(url): 15 r = requests.get(url) 16 r.encoding = r.apparent_encoding 17 return r.text if r.status_code == 200 else None 18 19 20 def parse_page(html): 21 html = etree.HTML(html) 22 forecasts = html.xpath(‘/html/body/div[8]/div[1]/div[1]/div[2]/ul/li/a[1]/@title‘) 23 forecasts = ‘\n‘.join(forecasts) 24 keys = html.xpath(‘/html/body/div[8]/div[2]/div[6]/ul/li/b/text()‘) 25 values = html.xpath(‘/html/body/div[8]/div[2]/div[6]/ul/li/a/p/text()‘) 26 day_info = {i: values[keys.index(i)] for i in keys} 27 message = ‘青哥哥今日提醒:\n\n‘ + ‘南京今日生活指数:\n‘ + ‘\n‘.join( 28 [‘{}: {}‘.format(i, day_info[i]) for i in day_info]) + ‘\n‘ * 3 + ‘南京主要地区天气预报:\n‘ + forecasts 29 return message 30 31 32 def get_time(): 33 time = datetime.now().strftime(‘%H:%M:%S‘) 34 return time 35 36 37 clock = ‘06:00:0{}‘ # 设置启动时间 38 interval = 3 # 设置时间间隔 39 time_zone = [clock.format(i) for i in range(interval)] 40 41 42 def main(my_lover): 43 url = ‘http://www.tianqi.com/nanjing/‘ 44 html = get_page(url) 45 message = parse_page(html) 46 my_lover.send(message) 47 48 49 if __name__ == ‘__main__‘: 50 my_lover = login() 51 print(‘waiting......‘) 52 while True: 53 time = get_time() 54 if time in time_zone: 55 main(my_lover) 56 print(time) 57 sleep(interval) 58 sleep(1) # 程序休眠一秒,减少cpu的压力
标签:name now() strftime tin form cas 启动 sea 函数
原文地址:https://www.cnblogs.com/ivy-blogs/p/10687037.html