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

appium爬取微信朋友圈 安卓模拟器版

程序员文章站 2022-07-12 22:13:15
...

环境: 安卓7,夜神模拟器,微信7.0.7

代码:

import os
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 连接手机
PLATFROM = 'Android'
DEVICE_NAME = '127.0.0.1:62001 device'
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):
        # 此处是一个坑,sleep时间如果设置的短,将会找不到元素,因为此时微信并没有启动
        time.sleep(10)
        el1 = 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")
        el1.click()
        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/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout")
        el2.click()
        self.crawl()


    def crawl(self):
        # 滑动点
        FLICK_START_X = 300
        FLICK_START_Y = 300
        FLICK_DISTANCE = 600
        while True:
            # 上滑
            self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
            # 当前页面显示的所有状态
            items = self.wait.until(
                EC.presence_of_all_elements_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[1]/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView')))
            # 遍历每条状态
            for item in items:
                try:
                    # 昵称
                    nickname = item.find_element_by_id('com.tencent.mm:id/bag').get_attribute('text')
                    # 正文
                    content = item.find_element_by_id('com.tencent.mm:id/f3o').get_attribute('text')
                    if content == '':
                        pass
                    else:
                        print(nickname+':', content)
                        f = open('contents.txt','a',encoding='utf-8')
                        f.write('%s' % nickname + ':' +'%s' % content)
                        f.write('\n' + '-------------------------------------------------------------------------------------------------------------------------------------------------' + '\n')
                        time.sleep(3)
                except NoSuchElementException:
                    pass


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

坑: 

1、遇到找不到元素的情况可以考虑一下app的启动问题(指定页面没有打开的话,是找不到元素的),可以加time.sleep()解决。

2、安卓5老是连接失败(Could not proxy command to remote server. Original error:socket hang up)(安卓7,安卓9没问题),但是可以获取到微信朋友圈的时间信息,安卓7以上,无法获取到时间信息。

appium爬取微信朋友圈 安卓模拟器版