本来是想尝试抓取微信公众号文章的,结果卡在webview上怎么都切换不过去。后来切过去了却又没办法拿到微信文章,所以放弃这个方法获取微信文章,暂时记录一下使用appium的过程
首先配置一定要配好
dc = dc_wechat = {
"platformName": "Android",
"deviceName": "59b445b8",
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI",
# 一定要带这个参数 不然会清空聊天记录
"noReset": True,
"newCommandTimeout": 3600,
# 切换webview时需要用到下面的参数
"chromeOptions": {
'androidProcess': 'com.tencent.mm:tools'
},
'unicodeKeyboard':True,
'resetKeyboard':True,
"recreateChromeDriverSessions": True,
'chromedriverExecutable':'C:/Users/xky/Desktop/chromedriver.exe',
}
其实appium最多用的是定位然后点击 最后写了死循环手机滑动往下不停获取朋友圈发的文本信息,
公众号文章抓取尝试了另一方法,这里简单做一个appium的分享 不说了 直接贴代码吧 一看就懂了
FIRST_ACCOUNT_NAME = "发现"
def main():
driver = appium.webdriver.Remote("http://localhost:4723/wd/hub", dc)
driver.implicitly_wait(300)
sample_element = driver.find_element_by_xpath(
f'//android.widget.TextView[@text="{FIRST_ACCOUNT_NAME}"]').click()
pyq = driver.find_element_by_xpath( f'//android.widget.TextView[@text="朋友圈"]')
pyq.click()
name = driver.find_element_by_class_name("android.widget.TextView")
time.sleep(10)
rid = name.get_attribute("resource-id") # 'com.tencent.mm:id/e3x'
print(rid)
accounts = set()
prev_count = -1
retry = 6
while True:
driver.swipe(300, 1000, 300, 300)
nickname = driver.find_element_by_id('com.tencent.mm:id/e3x').get_attribute('text')
print(nickname)
content = driver.find_element_by_id('com.tencent.mm:id/b_l').get_attribute('text')
print(content)
data = {'nickname': nickname, 'content': content}
print(data)
# self.collection.update({'nickname': nickname, 'content': content}, {'$set': data}, True)
print(list(accounts))
if __name__ == '__main__':
print(main())