大家好,我是烤鸭:
分享个微信自动化发送的新方式,仅技术分享。
本来是公众号文章抓取相关的,审核一直不过,将就看吧。
需要的工具
Java(jdk1.8)
Fiddler
Python(3.8)
一些获取链接的方式
内部链接:
https://blog.csdn.net/wnma3mz/article/details/78570580
三方API:
http://www.gsdata.cn/query/wx?q=%E5%B7%85%E5%B3%B0%E6%99%BA%E4%B8%9A
搜狗搜索:
http://weixin.sogou.com/weixin?type=1&query=%E5%A4%A7%E6%95%B0%E6%8D%AE%E6%96%87%E6%91%98&ie=utf8&sug=n&sug_type=
(会要求输入验证码)
自动化发送
修改 Fiddler,Rules—>Customize Rules, OnBeforeResponse 方法,拦截response写入
static function OnBeforeResponse(oSession: Session) {if (m_Hide304s && oSession.responseCode == 304) {oSession["ui-hide"] = "true";}//加在方法末尾if (oSession.HostnameIs("mp.weixin.qq.com") && oSession.uriContains("https://mp.weixin.qq.com/mp/getappmsgext")){var filename = "D:\\data\\wx\\fiddler-token.log";var curDate = new Date();var logContent = "[" + curDate.toLocaleString() + "] " + oSession.GetRequestBodyAsString() + "\r\n"+oSession.GetResponseBodyAsString()+"\r\n";var sw : System.IO.StreamWriter;if (System.IO.File.Exists(filename)){sw = System.IO.File.AppendText(filename);sw.Write(logContent);}else{sw = System.IO.File.CreateText(filename);sw.Write(logContent);}sw.Close();sw.Dispose();}}
利用python微信发送,自动点击发送内容,效果如图。
url就是列表接口获取的,放到txt文件
import time
import tracebackimport psutil
from pywinauto.application import Application
from time import sleep
from pywinauto import mouse
from pywinauto.keyboard import send_keys
from pywinauto import keyboard# 常用方式一:连接已有微信进程(进程号在 任务管理器-详细信息 可以查看)
global gPid, gApp, win_main_Dialog, win, search, search_cords
gPid = 0
search = Nonedef GetWeChatPID(name):# 获得全部进程的ID# pids=psutil.pids()pids = psutil.process_iter()# 遍历全部进程IDfor pid in pids:if (pid.name() == name):# 输出进程IDreturn pid.pid# 没有找到则输出0return print('请打开微信')def auto_play_wechat(url):print('gPid =' + str(globals()['gPid']) + '')global gPid, gApp, win_main_Dialog, win, search, search_cordsif globals()['gPid'] == 0:gPid = GetWeChatPID('WeChat.exe')gApp = Application(backend='uia').connect(process=gPid)# 拿到微信主窗口win_main_Dialog = gApp.window(class_name='WeChatMainWndForPC')win = gApp[u'微信']win.set_focus()# 定位到信息列表,获取这个对象conunacation = win_main_Dialog.child_window(title="会话", control_type="List")# 定位到这个位置position = conunacation.rectangle()mouse.click(button='left', coords=(position.left + 100, position.top + 10))send_keys(url, pause=0.005)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.com')[1]if text in str(mes):text_cord = mes.rectangle()mouse.click(coords=(text_cord.left + 150, text_cord.top + 50))# 最小化文件传输助手# win.maximize()sleep(2)win.close()# 鼠标复位# mouse.press(button='left',coords=(text_cord.left - 1500, text_cord.top - 20))# win_main_Dialog.minimize()def getUrlsByTxt(localUrl):with open(localUrl, "r") as f:seed = 1;for line in f.readlines():line = line.strip('\n') # 去掉列表中每一个元素的换行符print('seed=%d,line=%s' % (seed, line))try:auto_play_wechat(line)except Exception as e:traceback.print_exc()seed += 1if __name__ == '__main__':# auto_play_wechat('http://mp.weixin.qq.com/s?__biz=MjM5NTE1OTQyMQ==&mid=2650952200&idx=1&sn=b4302c5fd6fb356c71e561c5dc8bda7f&chksm=bd0a3e1a8a7db70c010d2ebdeec37432f303ead76e2ededfd34245414dcbf90e22093a04b7cc#rd')getUrlsByTxt('D:\\data\\wx\\urls_test.txt');
fiddler 拦截之后的输出
[2022年2月17日 22:41:44] r=0.8837538029416401&__biz=MjM5NzM0MTYyMw%3D%3D&appmsg_type=9&mid=2650518168&sn=b465db5c67ec66135eb746c28478aa3c&idx=3&scene=27&title=%25E7%25BB%259D%25E7%25BE%258E%25EF%25BC%2581%25E5%258D%2583%25E4%25BD%2599%25E4%25BB%25B6%25E4%25B8%25AD%25E5%258D%258E%25E7%2591%25B0%25E5%25AE%259D%25E4%25BA%25AE%25E7%259B%25B8%25E5%259B%25BD%25E5%25AE%25B6%25E7%25BA%25A7%25E6%2596%2587%25E5%258C%2596%25E6%25AE%25BF%25E5%25A0%2582&ct=1644238894&abtest_cookie=&devicetype=Windows%2010%20x64&version=63030073&is_need_ticket=0&is_need_ad=0&comment_id=2258995482893221894&is_need_reward=0&both_ad=0&reward_uin_count=0&send_time=&msg_daily_idx=1&is_original=0&is_only_read=1&req_id=1722XL3Y7mdpMii8yfbIJSkU&pass_ticket=g%2BhXr1LC9DCyiGiZCGxTkC6pBwoMhg%2BEX2txlFKR2zFpLQTWYeO3RzUnViAEnnm5&is_temp_url=0&item_show_type=0&tmp_version=1&more_read_type=0&appmsg_like_type=2&related_video_sn=&related_video_num=5&vid=&is_pay_subscribe=0&pay_subscribe_uin_count=0&has_red_packet_cover=0&album_id=1296223588617486300&album_video_num=5&cur_album_id=undefined&is_public_related_video=NaN&encode_info_by_base64=undefined&exptype=
{"advertisement_info":[],"appmsgstat":{"show":true,"is_login":true,"liked":false,"read_num":448,"like_num":1,"ret":0,"real_read_num":0,"version":1,"prompted":0,"like_disabled":false,"style":1,"video_pv":0,"video_uv":0,"friend_like_num":0,"old_liked":false,"old_like_num":2,"show_gray":0,"old_liked_before":0,"show_like":1,"show_like_gray":0,"show_old_like":1,"show_old_like_gray":0,"show_read":1},"comment_enabled":1,"reward_head_imgs":[],"only_fans_can_comment":false,"comment_count":0,"is_fans":1,"nick_name":"ccc","logo_url":"http:\/\/mmsns.qpic.cn\/mmsns\/iaxNB5XaibCeLTYWIUGCYm7cS1kFxTx4ibUSEBZJ6VnOdXPDItJ9PaGRg\/0","friend_comment_enabled":1,"base_resp":{"wxtoken":777},"more_read_list":[],"friend_subscribe_count":0,"related_tag_article":[],"original_article_count":0,"video_share_page_tag":[],"related_tag_video":[],"only_fans_days_can_comment":false,"appmsg_album_videos":[],"test_flag":0,"video_continue_flag":1,"public_tag_info":{"tags":[]},"share_flag":{"show":1,"show_gray":0},"appmsgact":{"old_liked_before":0,"seen_before":0,"share_before":0,"favorite_before":0,"reward_before":0,"pay_before":0}}
解析文本 ReaderTxt
package com.machu.picchu.crawler.utils;import com.alibaba.fastjson.JSONObject;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;/*** 读取Fiddler写入的内容,并将结果写入数据库* @author Administrator**/
public class ReaderTxt {
// DBUtil dbu=new DBUtil();public static void main(String[] args){ReaderTxt rt=new ReaderTxt();ArrayList<String> list=rt.InitTxt();for(int i=0;i<list.size();i++){System.out.println("dbUtil readTxt inum="+i);///mp/getappmsgext?f=json&mock=&uin=777&key=777&pass_ticket=&wxtoken=777&devicetype=Windows10&clientversion=62060833&appmsg_token=1018_nqJWTVcEH20DIl%252Fh4-0maWDSZTGyPYXvkZxC7pYdkfUq6veSGq2rEbgTDV9pLGv0UXuxxPipiSVTela1&x5=0&f=jsonString urlmsg=list.get(i);if(!urlmsg.contains("appmsg_type")){System.out.println("dbUtil readTxt inum="+i+" ,no appmsg_type");continue;}int start=urlmsg.indexOf("&sn=")+4;urlmsg=urlmsg.substring(start);int end=urlmsg.indexOf("&");urlmsg=urlmsg.substring(0, end);String info=list.get(i+1);if(!info.contains("appmsgstat")){System.out.println("dbUtil readTxt inum="+i+" ,no appmsgstat");continue;}JSONObject json=JSONObject.parseObject(info);if(json.containsKey("appmsgstat")){JSONObject appmsgstat=json.getJSONObject("appmsgstat");int read_num=appmsgstat.getInteger("read_num");int like_num=appmsgstat.getInteger("like_num");int old_like_num=appmsgstat.getInteger("old_like_num");}}
// rt.dbu.destroy();}public ArrayList<String> InitTxt(){ArrayList<String> list=new ArrayList<String>();try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw /* 读入TXT文件 */ String pathname = "D:\\data\\wx\\fiddler-token.log"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径File filename = new File(pathname);InputStreamReader reader = new InputStreamReader( new FileInputStream(filename),"utf-8"); // 建立一个输入流对象reader BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = ""; while (line != null) { line = br.readLine(); // 一次读入一行数据 if(line==null){ break;}list.add(line);} } catch (Exception e) { e.printStackTrace(); } return list;}
}
总结
仅限个人学习,有很多不完善的地方。