selenium配合PhantomJS()再加上Image函数的crop功能,把网页截图,指定大小;
然后再用itchat给指定人指定时间发送
#coding:utf-8
'''
https://github.com/hzlRises/hzlgithub/blob/master/weixin/weather.py
'''
import itchat,time
from PIL import Image
from selenium import webdriver
from urllib import quote
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def save_jpg():
city_list = ['beijingtianqi','chengdutianqi','handantianqi','xiamentianqi']
for index,city in enumerate(city_list):
time.sleep(1)
encode_city = quote(city)
url = 'https://www.baidu.com/s?ie=UTF-8&wd=%s'%encode_city
picName = '%s.png'%index
browser = webdriver.PhantomJS()
browser.get(url)
browser.maximize_window()
browser.save_screenshot(picName)#保存截图
#获取天气模板的位置、尺寸大小
imgelement = browser.find_element_by_xpath('//*[@id="1"]')
location = imgelement.location#获取天气x,y轴坐标
size = imgelement.size#获取天气的长宽
rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']-20))
i = Image.open(picName) #打开0.png
tinaqi = i.crop(rangle)#使用Image的crop函数,从截图中再次截取我们需要的
tinaqi.save('send_%s.png'%index)
browser.close()
#发送
user_content = itchat.search_friends(name=u'雨一直下')
userName = user_content[0]['UserName']
itchat.send_image('send_%s.png'%index,toUserName = userName)
user_content_baby = itchat.search_friends(name=u'')
userName_baby = user_content_baby[0]['UserName']
itchat.send_image('send_%s.png'%index,toUserName = userName_baby)
def main():
itchat.auto_login(hotReload=True)
while True:
time.sleep(1)
current_time = time.localtime(time.time())
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
if ((current_time.tm_hour == 7) and (current_time.tm_min) == 30 and (current_time.tm_sec == 0)):
save_jpg()
if __name__ == '__main__':
main()