├── README.md └── run.py /README.md: -------------------------------------------------------------------------------- 1 | #### Setup1: 运行前先连接手机,打开开发者模式 2 | #### Setup2: 在打开appium 3 | #### Setup3: 设置用户名密码, 这里使用的是QQ登陆 4 | ``` 5 | # deviceName请根据自身手机进行设置 6 | # 如果不想每次都登陆,请修改desired_caps为: 7 |       desired_caps = { 8 | "platformName": "Android", 9 | "deviceName": "MI_5s_Plus", 10 | "appPackage": "com.tencent.mm", 11 | "appActivity": ".ui.LauncherUI", 12 | "noReset": True, 13 | } 14 | # 然后把run方法里的login模块注释掉即可 15 | ``` 16 | ##### TODO:去重&存储 17 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import time 4 | from appium import webdriver 5 | from selenium.common.exceptions import NoSuchElementException 6 | from selenium.webdriver.common.by import By 7 | from selenium.webdriver.support.ui import WebDriverWait 8 | from selenium.webdriver.support import expected_conditions as EC 9 | 10 | 11 | username = "" 12 | password = "" 13 | 14 | 15 | class Moments(object): 16 | def __init__(self): 17 | # 驱动配置 18 | server = "http://localhost:4723/wd/hub" 19 | desired_caps = { 20 | "platformName": "Android", 21 | "deviceName": "MI_5s_Plus", 22 | "appPackage": "com.tencent.mm", 23 | "appActivity": ".ui.LauncherUI" 24 | } 25 | self.driver = webdriver.Remote(server, desired_capabilities=desired_caps) 26 | self.wait = WebDriverWait(self.driver, 30) 27 | 28 | def login(self): 29 | """ 30 | 登陆微信 31 | :return: 32 | """ 33 | dl = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/d1w"))) 34 | dl.click() 35 | # 点击用qq号登陆 36 | qq = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/bwm"))) 37 | qq.click() 38 | # passwd = self.driver.find_elements_by_id("com.tencent.mm:id/hx") 39 | # 输入账号 40 | user_passwd = self.driver.find_elements_by_id("com.tencent.mm:id/hx") 41 | user = user_passwd[0] 42 | passwd = user_passwd[1] 43 | user.set_text(username) 44 | # 输入密码 45 | passwd.set_text(password) 46 | # 点击登陆按钮 47 | submit = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/bwn"))) 48 | submit.click() 49 | # 不匹配通讯录 50 | alk = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/alk"))) 51 | alk.click() 52 | 53 | def enter(self): 54 | # 点击"发现"选项卡 55 | finds = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[@resource-id='com.tencent.mm:id/ayn']"))) 56 | finds[2].click() 57 | # 进入朋友圈 58 | friend = self.wait.until(EC.presence_of_element_located((By.ID, "com.tencent.mm:id/a9d"))) 59 | friend.click() 60 | 61 | def crawl(self): 62 | flick_start_x = 300 63 | flick_start_y = 300 64 | flick_distance = 700 65 | while True: 66 | self.driver.swipe(flick_start_x, flick_start_y+flick_distance, flick_start_x, flick_start_y) 67 | self.get_page_info() 68 | 69 | def get_page_info(self): 70 | items = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[@resource-id='com.tencent.mm:id/ddn']//android.widget.LinearLayout"))) 71 | for item in items: 72 | try: 73 | # 昵称 74 | nickname = item.find_element_by_id("com.tencent.mm:id/apv").get_attribute("text") 75 | # 正文 76 | content = item.find_element_by_id("com.tencent.mm:id/deq").get_attribute("text") 77 | print("{}---{}".format(nickname, content.replace("\n", ""))) 78 | except NoSuchElementException: 79 | pass 80 | 81 | def run(self): 82 | # 登陆微信 83 | self.login() 84 | # 进入朋友圈 85 | self.enter() 86 | # 抓取信息模拟上滑 87 | self.crawl() 88 | 89 | 90 | if __name__ == "__main__": 91 | m = Moments() 92 | m.run() 93 | --------------------------------------------------------------------------------