⽂章全⾃动采集(使⽤mitmproxy抓包,然后⽤pywinauto实现⾃动
点击。。。
本⽂章仅供学习和参考
因为的cookies和app_msg会过期
这篇⽂章主要是提供⾃动获取cookies和app_msg的过程
实现了cookies和app_msg的获取很容易就实现的⾃动采集
只需要先登录pc端就可以
⼀.⽬前的流程
1.使⽤公众平台,进⾏扫码登录,获取历史⽂章链接
2.开启mitmproxy代理
3.使⽤pywinauto模块实现pc端⾃动对⽂件传输助⼿发送⽂章url并点击,从⽽实现获取pc端cookies和appmsg_token ⼆.实现
第⼀步⽹上有许多教程,在这⾥就不说了
主要是需要实现mitmproxy⾃动抓包和pc端⾃动点击
mitmproxy⾃动抓包:
创建write_cookies.py
import urllib
import sys
from mitmproxy import ctx
from mitmproxy import io, http
# command: mitmdump -s write_cookies.py -w dat mp.weixin.qq/mp/getappmsgext
class WriterCookie:
"""
mitmproxy的监听脚本,写⼊cookie和url到⽂件
"""
def__init__(self,outfile:str)->None:
self.f =open(outfile,"w")
def response(self, flow: http.HTTPFlow)->None:
"""
完整的response响应
:param flow: flow实例,
"""
# 获取url
url = urllib.parse.quest.url)
# 将url和cookie写⼊⽂件
if"mp.weixin.qq/mp/getappmsgext"in url:
self.f.write(url +'\n')
self.f.write(kies))
self.f.close()
# 退出
exit()
# 第四个命令中的参数
addons =[WriterCookie(sys.argv[4])]
``
再创建get_wechat_cookies.py⽂件
open.bat⽂件
@echo off
echo 开始设置IE代理上⽹
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyEnable /t REG_DWORD /d 1/f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyServer /d "127.0.0.1:8080"/f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyOverride /t REG_SZ /d "<-loopback>"/f echo 代理设置完成按任意键关闭
close.bat
@echo off
echo 开始清除IE代理设置
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyEnable /t REG_DWORD /d 0/f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyServer /d ""/f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"/v ProxyOverride /f
echo IE代理清楚完成按任意键关闭
import re
import os
from wechat_auto import auto_play_wechat
import threading
import time
class ReadCookie(object):
"""
启动write_cookie.py 和解析cookie⽂件,
"""
def__init__(self,outfile):
self.outfile = outfile
print(self.outfile)
def parse_cookie(self):
"""
解析cookie
:return: appmsg_token, biz, cookie_str·
"""
with open(self.outfile)as f:
data = f.read()
appmsg_token_string = re.findall("appmsg_token.+?&", data)[0].split('=')[1]
cookies = re.findall(r"\['(.*?)'\]", data)
Wechat_cookies ={}
for cookie in cookies:
cookie = cookie.split("', '")
Wechat_cookies[cookie[0]]= cookie[1]
return appmsg_token_string,Wechat_cookies
def write_cookie(self):
"""
启动 write_cookies。py
:
return:
"""
#当前⽂件路径
path = os.path.split(alpath(__file__))[0]
#path = os.getcwd()
print(path)
# mitmdump -s 执⾏脚本 -w 保存到⽂件本命令
command ="mitmdump -s {}/write_cookies.py -w {} mp.weixin.qq/mp/getappmsgext".format(
path,self.outfile)
os.system(command)
class MyThread(threading.Thread):
def__init__(self, func, args=()):
super(MyThread, self).__init__()
self.func = func
self.args = args
def run(self):
time.sleep(2)
def get_result(self):
threading.Thread.join(self)# 等待线程执⾏完毕
try:
sult
except Exception:
return None
def get_cookie():
os.system('open.bat')
rc = ReadCookie('')
rc.write_cookie()
os.system('close.bat')
appmsg_token_string, cookies = rc.parse_cookie()
return appmsg_token_string, cookies
url='mp.weixin.qq/s?__biz=MjM5MjAxNDM4MA==&mid=2666430514&idx=2&sn=effd2a2e000791d6ac52451dc81223d9&chksm=bdb6e9718ac 1606710570156d51879e0b2dcf1351bf9a0eea8002b59f74808ff218e17eae6de#rd'
def get_result(url):
more_th1 = MyThread(get_cookie)
more_th2 = MyThread(auto_play_wechat, args=(url,))
more_th1.start()
more_th2.start()
more_th1.join()
more_th2.join()
appmsg_token_string = _result()[0]
cookies = _result()[1]
return appmsg_token_string,cookies
get_result(url)
⾃动在⽂件传输助⼿发送url
wechat_auto.py这个⽂件可以实现⾃动发送url,并点击
import time
import psutil
from pywinauto.application import Application
from time import sleep
from pywinauto import mouse
from pywinauto.keyboard import send_keys
from pywinauto import keyboard
# 常⽤⽅式⼀:连接已有进程(进程号在任务管理器-详细信息可以查看)
def GetWeChatPID(name):
# 获得全部进程的ID
# pids=psutil.pids()
pids = psutil.process_iter()
# 遍历全部进程ID
for pid in pids:
if(pid.name()==name):
# 输出进程ID
return pid.pid
# 没有到则输出0
return print('请打开')
def auto_play_wechat(url):
pid = GetWeChatPID('')
app = Application(backend='uia').connect(process=pid)
# 拿到主窗⼝
win_main_Dialog = app.window(class_name='WeChatMainWndForPC')
win = app[u'']
# 将窗⼝最⼤化
win.maximize()
search = win_main_Dialog.child_window(title=u"搜索", control_type="Edit")
# 获得搜索框的位置信息
search_cords = angle()
mouse.click(button='left', coords=(search_cords.left +100, p +10))
mouse.click(button='left', coords=(search_cords.left +100, p +10))
mouse.click(button='left', coords=(search_cords.left +100, p +10))
# 输⼊名称⽂件传输助⼿
send_keys('⽂件传输助⼿')
time.sleep(2)
# 按Enter键,进⼊聊天界⾯
send_keys('{ENTER}')
send_keys(url)
send_keys('{ENTER}')
mes = win_main_Dialog.child_window(title=u"消息", control_type="List")
mes_list = mes.items()
for i,mes in enumerate(mes_list):
text = url.split('mp.weixin.qq')[1]
if text in str(mes):
text_cord = angle()
mouse.click(coords=(text_cord.left+1500, p +20))
#最⼩化
connect和join的区别win.minimize()
sleep(2)
win_main_Dialog.minimize()
#auto_play_wechat('mp.weixin.qq/s?__biz=MjM5NTE1OTQyMQ==&mid=2650952200&idx=1&sn=b4302c5fd6fb356c71e561c5dc8bda7f&chksm =bd0a3e1a8a7db70c010d2ebdeec37432f303ead76e2ededfd34245414dcbf90e22093a04b7cc#rd')
当上⾯3个⽂件都创建好了之后,运⾏get_wechat_cookies.py
就可以得到传⼊⽂章的app_msg和cookies
当app_msg和cookies过期时在⾃动获取就好了,这样就可以实现全⾃动采集⽂章的点赞和评论了
全部的代码就不放了,这⾥已经把⾃动获取app_msg和cookies的代码给出了,后⾯的很简单就可以实现了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论