微信运动红心点赞,自动点赞,本python脚本是通过 uiautomator2 类库实现的

#!/use/bin/evn python
# -*- coding: utf-8 -*-
# project: pythonProject wx_sports_hearts
# author: honey
# date: 2021/10/24

"""
使用说明:
测试环境:小米手机8, python3.8 , mac系统,微信 8.0.15 版本
1.安装:
    pip install --pre uiautomator2
    pip install weditor

2.初始化客户端和服务端
    a.通过数据线连接手机
    b.手机开启USB调试,模拟点击,允许USB安装
    c.执行下面的初始化操作
        python -m uiautomator2 init
3.打开web界面,该界面主要是为了获取控件的ID 【自己开发调试使用,如果只点赞,略过即可】
    python -m weditor

4.打开微信,点击微信运动,点击步数排行榜,然后开始运行该程序即可。
"""
import uiautomator2 as u2
from uiautomator2.exceptions import InjectPermissionError

d = u2.connect()
d.implicitly_wait(0.2)
rid = '//*[@resource-id="com.tencent.mm:id/c68"]'
LLayout = '/android.widget.LinearLayout'
RLayout = '/android.widget.RelativeLayout'
TView = '/android.view.View'

rid_RLayout = rid + RLayout
LLayout1_RLayout2 = LLayout + "[1]" + RLayout + "[2]"
LLayout1_RLayout1 = LLayout + "[1]" + RLayout + "[1]"
Text_RLayout = RLayout + "[1]" + TView + "[1]"
i = 1
position = []
ele_index_list = []
mine_name = d.xpath(rid_RLayout + '[2]' + LLayout1_RLayout2 + RLayout + "[1]" + Text_RLayout).get_text()

print('当前用户昵称:', mine_name)
black_name_list = [
    mine_name,
    '赵二狗'
]
love_count = 0
while i < 20:
    try:
        ele_path = d.xpath(rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout2 + RLayout + "[2]")
        ele_name = d.xpath(
            rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout2 + RLayout + "[1]" + Text_RLayout).get_text()
        ele_index = d.xpath(
            rid_RLayout + "[" + str(i) + "]" + LLayout1_RLayout1 + RLayout + "[1]/android.widget.TextView").get_text()

        x, y = ele_path.center()
        if i == 1:
            position = [[x, y], [x, y]]
        else:
            if len(position) == 0:
                position = [[x, y], [x, y]]
            position[1] = [x, y]
        if ele_path.exists:
            if ele_index in ele_index_list:
                continue
            ele_index_list.append(ele_index)
            if ele_name in black_name_list:
                continue
            ele_path.click()
            love_count += 1
            print(ele_name, '微信运动已点赞')
    except InjectPermissionError as e:
        print(e)
        exit(0)
    except Exception:
        if len(position) == 2 and position[0][1] != position[1][1]:
            try:
                if d(resourceId="com.tencent.mm:id/dzn").get_text() == '邀请朋友':
                    break
            except Exception:
                pass
            print('当前页面已经点赞完成,继续下一页')
            d.drag(position[1][0], position[1][1], position[0][0], position[0][1])
            i = 0
            position = []
    finally:
        i += 1
print('点赞结束', love_count)

眨眼中,红心赞统统完成,太棒喽

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐