壞境: 安卓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()
成果: