微信公众号抓取 操作手机相关操作 ②

上篇文章:https://blog.csdn.net/hhaimm/article/details/124174951?spm=1001.2014.3001.5501(环境搭建及部分anyproxy 代码)

本文主要介绍通过python代码进行操作手机
话不多说,直接上代码:

需要引入的包

import datetime
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
from win32com.client import GetObject

创建手机连接(通过 appium)

 desired_caps = {
        "platformName": "Android",
        "deviceName": "3cc56469", #小米933
        "appPackage": "com.tencent.mm",
        "appActivity": ".ui.LauncherUI",
        "automationName": "Uiautomator2",

        # "dontStopAppOnReset" : "True", #不重启 app
        "unicodeKeyboard": "True",  # 使用unicodeKeyboard的编码方式来发送字符串 ,可以实现输入中文
        "resetKeyboard": "True",  # 隐藏虚拟键盘,防止遮挡元素
        "newCommandTimeout": "86400",  # 设置命令超时时间(程序默认启动时间为60秒,60秒无操作程序自动关闭),单位:秒
        "noReset": "True",
        "chromeOptions": {
            "androidProcess": "com.tencent.mm:tools" # 微信APP
        }
    }

基于微信 app 7.0.22 版本(默认状态为当前微信已登录,直接进行登录切换操作)

  # popen返回文件对象,跟open操作一样
        os.system("start anyproxy -i")
        time.sleep(10)
        cursors = conn.cursor(pymysql.cursors.DictCursor)
        server = 'http://localhost:4723/wd/hub'
        # app启动参数

        print("开始切换账号--------------------")
        # 驱动
        driver = webdriver.Remote(server, desired_caps)
        wait = WebDriverWait(driver, 30)
        time.sleep(10)
        # 点击 状态 我
        main_until = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                "//android.widget.FrameLayout[@content-desc=\"当前所在页面,与的聊天\"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[4]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageView")))
        main_until.click()
        time.sleep(10)
        # 点击 设置
        setting_until = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                   "//android.widget.FrameLayout[@content-desc=\"当前所在页面,与的聊天\"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[5]/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout")))
        setting_until.click()
        time.sleep(10)

        # 点击 退出
        quit_util = wait.until(EC.presence_of_element_located((By.XPATH,
                                                               "//android.widget.FrameLayout[@content-desc=\"当前所在页面,设置\"]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.ListView/android.widget.LinearLayout[12]/android.widget.LinearLayout")))
        quit_util.click()
        time.sleep(10)

        # 点击退出登录
        quit_logging_util = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                       "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[1]/android.widget.RelativeLayout")))
        quit_logging_util.click()
        time.sleep(10)
        # 点击退出
        sign_out = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/doz")))
        sign_out.click()
        time.sleep(15)
        # 点击 更多
        more_util = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/d60")))
        more_util.click()
        time.sleep(10)
        # 点击 登陆其他账号
        login_other_account = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                         "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[1]/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.TextView")))
        login_other_account.click()
        time.sleep(10)
        # 点击 使用微信号进行登陆
        login_weChat = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/d5u")))
        login_weChat.click()
        time.sleep(5)
        user_name = ""
        password = ""
         # 表结构(用户名,密码,is_used(0 未进行数据抓取 1以抓取过一次))
        sql_user = "SELECT * FROM wechat_user WHERE is_used = 0 LIMIT 1"
        cursors.execute(sql_user)
        user = cursors.fetchall()
        if user:
            user_name = user[0]['user_name']
            password = user[0]['user_password']
            # 输入账号
            set_user_name = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                       "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.EditText")))
            set_user_name.send_keys(user_name)
            # 输入密码
            set_password = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                      "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.EditText")))
            set_password.send_keys(password)

            # 点击登陆
            login_click = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/d5n")))
            login_click.click()
            update_user = "UPDATE wechat_user SET is_used= 1 WHERE id = '{}'".format(user[0]['id'])
            cursors.execute(update_user)
            conn.commit()
            time.sleep(30)
            i = 0
             # 滑动,有时候会有验证码(这个华东是粗略的,不准确)
            while i < 3:
                driver.swipe(200, 1000, 850, 1000)
                time.sleep(10)
                i += 1
            print("完成切换账号--------------------")

            # 点击搜索
            search_btn = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/f8y")))
            search_btn.click()
            print("点击页面搜索")

            # 输入文件传输助手
            print_btn = wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/bhn")))
            print_btn.send_keys("文件传输助手")
            print_btn_file = wait.until(EC.presence_of_element_located((By.XPATH,
                                                                        "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.RelativeLayout[2]/android.widget.LinearLayout/android.widget.LinearLayout")))
            print_btn_file.click()
            time.sleep(2)

            # 在文件传输助手中进行 首次请求点击(在文件传输助手中发送个消息,可访问的链接即可)
            driver.tap([(500, 600)], 0)
            flag = 0
            while flag < 20:
                driver.swipe(500, 1600, 500, 500, 75)
                strTime = time.localtime()
                strTime = datetime.datetime.fromtimestamp(strTime)
                print(strTime)
                print(flag)
                time.sleep(180)
                try:
                    driver.swipe(500, 1600, 500, 500, 75)
                    # 间隔5分钟点击一次返回键 防止程序
                    # 挂掉`8
                    driver.keyevent(4)

手机微信登录相关业务操作 完成
欢迎大家指导交流
下篇文章我会给大家接收数据解析相关操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值