吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2278|回复: 19
收起左侧

[Python 原创] uiautomation获取wx公众号文章

  [复制链接]
asqwe1 发表于 2023-12-13 15:47
本帖最后由 asqwe1 于 2023-12-13 15:56 编辑

[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-

import uiautomation as auto
import re
import time

'''
原理:模拟鼠标点击公众号文章,用微信自带的浏览器打开,然后右键复制链接。
个人练习uiautomation库的使用,获取公众号所有的文章链接,会将链接放到D盘url.txt中,文章标题放到D盘title.txt
然后配合火狐Save Page WE Options插件,批量保存为html,Save Page WE插件选项(Load Listed URLs From File.. ,Load lazy content before saving:scroll page,time..其它选项根据需要)
你还可以用python wget或requests写一段代码获取,效果可能没有火狐好
业余新手一枚,大佬可以根据需求自行修改代码,出错不要@我
中途如果你只想采一部分,不想继续了,可以关掉公众号窗口,程序会保存你已采过的链接。
如果程序出错关掉公众号窗口、cmd窗口快速点击并按下ctrl+c、键盘的win键然后键盘操作上下左右键TAB等键选择注销或关机
准备:
必须将微信升级到最新版本,v3.9.8.15,其它版本出错。
微信设置:去掉勾选“使用默认浏览器打开网页”、去掉自动升级
首先打开公众号的窗口,并且公众号顶部与底部可见且不要与任务栏重叠,尽可能少的打开其它窗口,以防干扰
'''
#auto.uiautomation.SetGlobalSearchTimeout(5)
public = auto.WindowControl(Name='公众号',Depth=1)
public.SetActive()
kuang = public.DocumentControl(Depth=2)  #主体框架

#摸拟鼠标操作,复制每篇文章的链接
def geturl():
    liulan = auto.PaneControl(Name='微信',ClassName='Chrome_WidgetWin_0',Depth = 1)
    url = []
    if liulan.Exists():
        liulan.SetActive()
        liulan.SetTopmost()
    for item, depth in auto.WalkControl(liulan, includeTop=True, maxDepth=15):
        if item.ControlType != auto.ControlType.TabItemControl:
            continue
        item.Click()
        item.RightClick()
        youjian = liulan.MenuItemControl(Name='复制链接',Depth = 6)
        #auto.SetClipboardText('')
        if youjian.Exists():
            youjian.Click()
            url.append(auto.GetClipboardText())
    liulan.ButtonControl(Name='关闭',Depth=4).Click()
    return url

def main(public,kuang):
    global lis
    global nam
    n = 0
    try:
        for item, depth in auto.WalkControl(public, includeTop=True, maxDepth=9):
            if depth !=9 or item.ControlType != auto.ControlType.TextControl or '已关注' in item.Name or '发消息' in item.Name:
                continue
            if auto.IsKeyPressed(auto.Keys.VK_F12):
                break
            print(item.Name)
            print(item.BoundingRectangle)
            nam.append(item.Name)
            public.SetActive()
            public.SetTopmost()
            #以下实现向下滚动,使文章可见并点击,会有个别文章遗漏没有点击,可尝试sleep时间长一点,
            while item.Exists() and item.BoundingRectangle.left == 0:
                kuang.WheelDown(waitTime=0.02)
                time.sleep(2)    
            item.Click()            
            #clic(public,item,kuang)
            #每打开5篇文章采集一下链接并关掉微信自带的浏览器
            n +=1
            if n % 5 == 0:
                try:
                    lis += geturl()
                except Exception:
                    pass
    except Exception:
                    pass        

#将链接与标题写到D盘
def opt2File(liss,ming=None):
    with open(f'D:/{ming}.txt','a') as f:
        for x in liss:
            f.write(x + '\n')


if __name__ == "__main__":
    try:
        lis = []
        nam = []
        main(public,kuang)
        opt2File(lis,'url')
        opt2File(nam,'title')
    except KeyboardInterrupt:
        print("end...")

初始状态

初始状态
22.jpg

免费评分

参与人数 7吾爱币 +14 热心值 +6 收起 理由
xaf + 1 + 1 用心讨论,共获提升!
hshcompass + 2 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Dannel1688 + 1 我很赞同!
helian147 + 1 + 1 热心回复!
弑神者91511 + 1 + 1 谢谢@Thanks!
debug_cat + 2 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xaf 发表于 2024-2-6 14:47
需要弄公众号链接的话,建议去注册一个公众号,公众号平台有超链接,可以直接查所有公众号的所有文章链接,比你这样慢慢粘贴效率快的多,
haiyanuser 发表于 2023-12-13 16:06
bing3076 发表于 2023-12-13 16:17
debug_cat 发表于 2023-12-13 17:16
工具不错啊 ,感谢分享。
turmasi1234 发表于 2023-12-13 17:16
用了一下不错,谢谢楼主的分享,
qumingyu286 发表于 2023-12-13 17:38
感谢,非常有用!
zxsbk 发表于 2023-12-13 17:56
思路不错,先学习了。
zeh521 发表于 2023-12-13 18:29
好东东!!感谢
yyzzy 发表于 2023-12-13 22:01
挺厉害的感谢楼主分享
www52pjzk 发表于 2023-12-14 10:05
实用分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 10:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表