欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

appium爬取微信朋友圈信息 真机测试

程序员文章站 2022-07-12 22:20:31
...

坏境: 安卓9,荣耀10,微信7.0.11

代码:

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC    # 可以通过xpath定位元素
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
import time
import re

# 连接手机
PLATFROM = 'Android'
DEVICE_NAME = 'COL_AL10'
APP_PACKGE = 'com.tencent.mm'
APP_ACTIVITY = '.ui.LauncherUI'
DRIVER_SERVER = 'http://localhost:4723/wd/hub'
TIMEOUT = 300



class Moments():
    def __init__(self):
        # 驱动配置
        self.desired_caps = {
            'platformName': PLATFROM,
            'deviceName': DEVICE_NAME,
            'appPackage': APP_PACKGE,
            'appActivity': APP_ACTIVITY,
            # 下面这句话不用重置微信了,直接登录手机上的微信
            "noReset": 'true'
        }
        self.driver = webdriver.Remote(DRIVER_SERVER, self.desired_caps)
        self.wait = WebDriverWait(self.driver, TIMEOUT)

    def enter(self):
        time.sleep(3)
        el2 = self.driver.find_element_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[3]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageView")
        el2.click()
        time.sleep(3)
        el3 = self.driver.find_element_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[1]/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout")
        el3.click()
        self.get_onepage_elementlist()


    def get_onepage_elementlist(self):
        FLICK_START_X = 300
        FLICK_START_Y = 300
        FLICK_DISTANCE = 810

        i=0
        contents = []
        while True:
            # 带图朋友圈配文和视频朋友圈配文
            content = self.driver.find_element_by_id("com.tencent.mm:id/f4m").get_attribute("text")
            print(content)
            i = i+1
            contents.append(content)
            time.sleep(2)
            # 上滑
            self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
            if i>= 107:
                break

        # 避免重复获取到的朋友圈信息,通过元祖进行避免
        for i in range(0, len(contents)-1):
            if contents[i] == contents[i+1]:
                pass
            else:
                f = open('content.txt','a',encoding='utf-8')
                f.write('%s' % contents[i])
                f.write('\n\n' + '-------------------------------------------------------------------------------------------------------------------------------------------------' + '\n\n')
        print('程序已结束')

if __name__ == "__main__":
    start = Moments()
    start.enter()

成果: 

appium爬取微信朋友圈信息 真机测试