|
| 1 | +import time |
| 2 | + |
| 3 | +from appium import webdriver |
| 4 | +from selenium.webdriver.common.by import By |
| 5 | +from selenium.webdriver.support.ui import WebDriverWait |
| 6 | +from selenium.webdriver.support import expected_conditions as EC |
| 7 | + |
| 8 | +class Wechat_Moment(): |
| 9 | + def __init__(self): |
| 10 | + desired_caps = {} |
| 11 | + desired_caps['platformName'] = 'Android' |
| 12 | + desired_caps['platformVersion'] = '5.1' |
| 13 | + desired_caps['deviceName'] = '88CKBM622PAM' |
| 14 | + desired_caps['appPackage'] = 'com.tencent.mm' |
| 15 | + desired_caps['appActivity'] = '.ui.LauncherUI' |
| 16 | + |
| 17 | + # 定义在朋友圈的时候滑动位置 |
| 18 | + self.start_x = 300 |
| 19 | + self.start_y = 800 |
| 20 | + self.end_x = 300 |
| 21 | + self.end_y = 300 |
| 22 | + |
| 23 | + # 启动微信 |
| 24 | + self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) |
| 25 | + # 设置等待 |
| 26 | + self.wait = WebDriverWait(self.driver, 300) |
| 27 | + print('微信启动...') |
| 28 | + |
| 29 | + |
| 30 | + def login(self): |
| 31 | + # 获取到登录按钮后点击 |
| 32 | + login_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/e4g"))) |
| 33 | + login_btn.click() |
| 34 | + # 获取使用微信号登录按钮 |
| 35 | + change_login_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/cou"))) |
| 36 | + change_login_btn.click() |
| 37 | + # 获取输入账号元素并输入 |
| 38 | + account = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/cos"]/android.widget.EditText'))) |
| 39 | + account.send_keys("wcnmwzs666") |
| 40 | + # 获取密码元素并输入 |
| 41 | + password = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/cot"]/android.widget.EditText'))) |
| 42 | + password.send_keys("jkluio789") |
| 43 | + # 登录 |
| 44 | + login = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/cov"))) |
| 45 | + login.click() |
| 46 | + # 点击去掉通讯录提示框 |
| 47 | + no_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/az9"))) |
| 48 | + no_btn.click() |
| 49 | + print('登录成功...') |
| 50 | + |
| 51 | + |
| 52 | + def find_xiaoshuaib(self): |
| 53 | + # 获取到搜索按钮后点击 |
| 54 | + search_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/iq"))) |
| 55 | + # 等搜索建立索引再点击 |
| 56 | + time.sleep(20) |
| 57 | + search_btn.click() |
| 58 | + # 获取搜索框并输入 |
| 59 | + search_input = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/kh"))) |
| 60 | + search_input.send_keys("wistbean") |
| 61 | + print('搜索小帅b...') |
| 62 | + # 点击头像进入 |
| 63 | + xiaoshuaib_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/py"))) |
| 64 | + xiaoshuaib_btn.click() |
| 65 | + # 点击右上角...进入 |
| 66 | + menu_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/jy"))) |
| 67 | + menu_btn.click() |
| 68 | + # 再点击头像 |
| 69 | + icon_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/e0c"))) |
| 70 | + icon_btn.click() |
| 71 | + # 点击朋友圈 |
| 72 | + moment_btn = self.wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/d86"))) |
| 73 | + moment_btn.click() |
| 74 | + print('进入朋友圈...') |
| 75 | + |
| 76 | + def get_data(self): |
| 77 | + while True: |
| 78 | + # 获取 FrameLayout |
| 79 | + items = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.tencent.mm:id/eew'))) |
| 80 | + # 滑动 |
| 81 | + self.driver.swipe(self.start_x, self.start_y, self.end_x, self.end_y, 2000) |
| 82 | + #遍历获取 |
| 83 | + for item in items: |
| 84 | + moment_text = item.find_element_by_id('com.tencent.mm:id/kt').text |
| 85 | + day_text = item.find_element_by_id('com.tencent.mm:id/eke').text |
| 86 | + month_text = item.find_element_by_id('com.tencent.mm:id/ekf').text |
| 87 | + print('抓取到小帅b朋友圈数据: %s' % moment_text) |
| 88 | + print('抓取到小帅b发布时间: %s月%s' % (month_text, day_text)) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + wc_moment = Wechat_Moment() |
| 92 | + wc_moment.login() |
| 93 | + wc_moment.find_xiaoshuaib() |
| 94 | + wc_moment.get_data() |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
0 commit comments