Python爬蟲學習筆記(八) — 手機爬蟲Appium

安裝環境

1、node.js 和 npm

2、JDK

java 環境,Android需要這個環境

3、Android SDK

4、Android AVD

5、Appium

6、Appium-Python-Client

python客戶端,用來連接Appium

7、Python

參考以下文章進行安裝

https://mp.weixin.qq.com/s?__biz=MzU2ODYzNTkwMg==&mid=2247484358&idx=1&sn=23e920d7a8d43dafd7607c8d30eeb946&scene=19#wechat_redirect

爬取手機微信的朋友圈

https://mp.weixin.qq.com/s?__biz=MzU2ODYzNTkwMg==&mid=2247484386&idx=1&sn=7f0545f27f095f20d69deedfa9f606a1&scene=19#wechat_redirect

import timefrom 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
class Wechat_Moment():
def __init__(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '88CKBM622PAM'
desired_caps['appPackage'] = 'com.tencent.mm'
desired_caps['appActivity'] = '.ui.LauncherUI'
# 定义在朋友圈的时候滑动位置
self.start_x = 300
self.start_y = 800
self.end_x = 300
self.end_y = 300
# 启动微信
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置等待
self.wait = WebDriverWait(self.driver, 300)
print('微信启动...')
def login(self):
# 获取到登录按钮后点击
login_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/e4g")))
login_btn.click()
# 获取使用微信号登录按钮
change_login_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/cou")))
change_login_btn.click()
# 获取输入账号元素并输入
account = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/cos"]/android.widget.EditText')))
account.send_keys("xxxxxxxx")
# 获取密码元素并输入
password = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/cot"]/android.widget.EditText')))
password.send_keys("xxxxxx")
# 登录
login = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/cov")))
login.click()
# 点击去掉通讯录提示框
no_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/az9")))
no_btn.click()
print('登录成功...')
def find_xiaoshuaib(self):
# 获取到搜索按钮后点击
search_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/iq")))
# 等搜索建立索引再点击
time.sleep(10)
search_btn.click()
# 获取搜索框并输入
search_input = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/kh")))
search_input.send_keys("wistbean")
print('搜索小帅b...')
# 点击头像进入
xiaoshuaib_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/py")))
xiaoshuaib_btn.click()
# 点击右上角...进入
menu_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/jy")))
menu_btn.click()
# 再点击头像
icon_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/e0c")))
icon_btn.click()
# 点击朋友圈
moment_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/d86")))
moment_btn.click()
print('进入朋友圈...')
def get_data(self):
while True:
# 获取 ListView
items = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.tencent.mm:id/eew')))
# 滑动
self.driver.swipe(self.start_x, self.start_y, self.end_x, self.end_y, 2000)
#遍历获取每个List数据
for item in items:
moment_text = item.find_element_by_id('com.tencent.mm:id/kt').text
day_text = item.find_element_by_id('com.tencent.mm:id/eke').text
month_text = item.find_element_by_id('com.tencent.mm:id/ekf').text
print('抓取到小帅b朋友圈数据: %s' % moment_text)
print('抓取到小帅b发布时间: %s月%s' % (month_text, day_text))
if __name__ == '__main__':
wc_moment = Wechat_Moment()
wc_moment.login()
wc_moment.find_xiaoshuaib()
wc_moment.get_data()

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store