一沸伏、背景
因業(yè)務(wù)需要獲取風(fēng)險(xiǎn)經(jīng)濟(jì)事件并采取應(yīng)對(duì)措施边臼,但因?yàn)榉N種原因又疏忽于每天去查看財(cái)經(jīng)日歷,于是通過(guò)爬取金十?dāng)?shù)據(jù)網(wǎng)站并自動(dòng)推送到微信查看。
二拆祈、目標(biāo)實(shí)現(xiàn)
image
三恨闪、環(huán)境與工具
1、pycharm:python開(kāi)發(fā)IDE
2放坏、windows 窗口句柄獲取工具 https://www.jb51.net/softs/584495.html
四咙咽、實(shí)現(xiàn)思路
爬蟲(chóng)獲取風(fēng)險(xiǎn)事件,然后python通過(guò)句柄定位到微信窗口淤年,模擬鍵鼠操作本機(jī)微信客戶端發(fā)給好友(自己)钧敞,最開(kāi)始本來(lái)使用微信itchat庫(kù),但是這個(gè)是基于微信網(wǎng)頁(yè)版麸粮,登錄幾次后微信提示:為了你的帳號(hào)安全,此微信號(hào)已不允許登錄網(wǎng)頁(yè)微信溉苛。你可以使用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('一次爬取結(jié)束-----等待下次循環(huán)爬取')
def parse(self, response):
#獲取風(fēng)險(xiǎn)事件列表
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'<i class="jin-star_active.*style="width:(.*)%;">', content.extract(),re.I)
lljd = content.extract().__contains__("利率決")
if searchObj or lljd:
if int(searchObj.group(1)) >= 80 or lljd: #風(fēng)險(xiǎn)等級(jí)達(dá)到4星或者是利率決定
# print("searchObj.group(1) : ", searchObj.group(1))
#<p class="jin-table_alignLeft">美國(guó)至3月20日美聯(lián)儲(chǔ)利率決定(上限)</p>
searchObj = re.search(r'<p.*>[\s\r\n]+([\u4e00-\u9fa50-9a-zA-Z]+)', content.extract(), re.I)
event = ""
timeS = ""
if searchObj :
event = searchObj.group(1)
# print("event : ", event)
#<td rowspan="2" class="jin-rili_content-time">02:00</td>
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點(diǎn)推送到微信
if datetime.now().hour == 8 or datetime.now().hour == 12 or datetime.now().hour == 19 :
self.sendMsgToWX(msg)
print(msg)
def sendMsgToWX(self, msg):
# 將微信放在前臺(tái)
win32gui.SetForegroundWindow(hwnd)
time.sleep(2)
# 將鼠標(biāo)移到(750, 700)
win32api.SetCursorPos((750, 700))
# 單擊左鍵獲取焦點(diǎn)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 750, 700, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 750, 700, 0, 0)
time.sleep(1)
# 將內(nèi)容寫(xiě)入到粘貼板
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, msg.encode(encoding='gbk'))
w.CloseClipboard()
time.sleep(1)
# 單擊鼠標(biāo)右鍵彈出上下文菜單
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 750, 700, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 750, 700, 0, 0)
time.sleep(1)
# 單擊鼠標(biāo)左鍵點(diǎn)擊粘貼
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)
# 按回車(chē)鍵發(fā)送
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)