7*24h新闻自动发送至微信(requests+pywinauto)
1. 爬取新闻列表
- 目标地址:https://kuaixun.eastmoney.com/
- 获取新闻列表
import time, requests, json
import pandas as pd
session = requests.session()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
session.get('https://kuaixun.eastmoney.com/', headers = headers)
res = session.get('https://np-weblist.eastmoney.com/comm/web/getFastNewsList?client=web&biz=web_724&fastColumn=102&sortEnd=&pageSize=10&req_trace=' + '%.0f'%(time.time()*1000) + '&_=' + '%.0f'%(time.time()*1000-2) + '&callback=jQuery18307434406413487331_' + '%.0f'%(time.time()*1000-200), headers = headers)
text = res.content.decode('utf-8')
df = pd.DataFrame(json.loads(text[len(text.split('(')[0]) + 1:-1])['data']['fastNewsList'])
df.sort_values(by='showTime', ascending=True, inplace = True)
list_news = (df['showTime'].str[5:7].astype('int').astype('str') + '.' + df['showTime'].str[8:10].astype('int').astype('str') + ' ' + df['showTime'].str[11:16] + ' ' + df['summary']).tolist()
print(list_news)
结果如下:
2. 发送至微信指定联系人
wechat_window = WindowControl(searchDepth=1, Name="微信", ClassName="WeChatMainWndForPC")
wechat_window.SetActive()
time.sleep(1)
keyboard.send_keys('^f')
keyboard.send_keys('7*24h滚动新闻')
time.sleep(1)
keyboard.send_keys('{ENTER}')
time.sleep(1)
for i in list_news:
wechat_window = WindowControl(searchDepth=1, Name="微信", ClassName="WeChatMainWndForPC")
wechat_window.SetActive()
time.sleep(1)
keyboard.send_keys(i)
keyboard.send_keys('{ENTER}')
with open('content.txt', 'w') as file:
file.write(content + i + '\n')
file.close()
time.sleep(1)
标签:df,pywinauto,keyboard,24h,keys,sleep,time,微信
From: https://blog.csdn.net/hbslsdfdf/article/details/136905979