一、背景
因业务需要获取风险经济事件并采取应对措施,但因为种种原因又疏忽于每天去查看财经日历,于是通过爬取金十数据网站并自动推送到微信查看。
二、目标实现
三、环境与工具
1、pycharm:python开发IDE
2、windows 窗口句柄获取工具 https://www.jb51.net/softs/584495.html
四、实现思路
爬虫获取风险事件,然后python通过句柄定位到微信窗口,模拟键鼠操作本机微信客户端发给好友(自己),最开始本来使用微信itchat库,但是这个是基于微信网页版,登录几次后微信提示:为了你的帐号安全,此微信号已不允许登录网页微信。你可以使用Windows微信或Mac。
五、主要代码
# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
from scrapy import signals
import win32con,win32gui,time,win32api
import win32clipboard as w
import re
from datetime import datetime
hwnd = 394916 #微信窗口句柄,使用句柄工具获取
class JinshiSpider(scrapy.Spider):
name = 'jinshirili'
allowed_domains = ['jinshi.com']
start_urls = ['https://rili.jin10.com/']
@classmethod
def from_crawler(cls, crawler, *args, **kwargs):
spider = super(JinshiSpider, cls).from_crawler(crawler, *args, **kwargs)
options = webdriver.ChromeOptions()
options.add_argument('--headless')
spider.chrome = webdriver.Chrome(chrome_options=options)
crawler.signals.connect(spider.spider_closed, signal=signals.spider_closed)
return spider
def spider_closed(self, spider):
spider.chrome.quit()
print('一次爬取结束-----等待下次循环爬取')
def parse(self, response):
#获取风险事件列表
contents = response.xpath('//div[@class="jin-rili_content J_rili_content"]//tr')
# print(len(contents))
for i,content in enumerate(contents):
searchObj = re.search(r'', content.extract(),re.I)
lljd = content.extract().__contains__("利率决")
if searchObj or lljd:
if int(searchObj.group(1)) >= 80 or lljd: #风险等级达到4星或者是利率决定
# print("searchObj.group(1) : ", searchObj.group(1))
#美国至3月20日美联储利率决定(上限)
searchObj = re.search(r'[\s\r\n]+([\u4e00-\u9fa50-9a-zA-Z]+)', content.extract(), re.I)
event = ""
timeS = ""
if searchObj :
event = searchObj.group(1)
# print("event : ", event)
#02:00
searchObj = re.search(r'time.*>([0-9:]+)<', content.extract(), re.I)
if searchObj:
timeS = searchObj.group(1)
# print("time : ", time)
msg = event+" "+timeS
# self.sendMsgToWX(msg)
#8 12 19点推送到微信
if datetime.now().hour == 8 or datetime.now().hour == 12 or datetime.now().hour == 19 :
self.sendMsgToWX(msg)
print(msg)
def sendMsgToWX(self, msg):
# 将微信放在前台
win32gui.SetForegroundWindow(hwnd)
time.sleep(2)
# 将鼠标移到(750, 700)
win32api.SetCursorPos((750, 700))
# 单击左键获取焦点
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 750, 700, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 750, 700, 0, 0)
time.sleep(1)
# 将内容写入到粘贴板
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, msg.encode(encoding='gbk'))
w.CloseClipboard()
time.sleep(1)
# 单击鼠标右键弹出上下文菜单
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 750, 700, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 750, 700, 0, 0)
time.sleep(1)
# 单击鼠标左键点击粘贴
win32api.SetCursorPos((770, 720))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 770, 720, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 770, 720, 0, 0)
time.sleep(1)
# 按回车键发送
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
六、源码地址
https://github.com/wu123456ming/jin10ToWX