环境配置
1.Java SDK下载及环境配置
java SDK下载:https://www.oracle.com/java/technologies/downloads/(科学上网) 配置JAVA_HOME:这是java sdk存放路径 配置path:将java sdk的bin目录加入到path中 验证:java/javac
2.Android SDK安装及环境配置
AndROid SDK下载:自己找去把 环境配置:1.新建一个ANDROID_HOME,将android sdk路径加入变量值 ? ? ? ??2.将android sdk中的platform-tools加入到path中 验证:命令行敲adb
XPath定位技术
W3CXpath教程 注意:相较于web端的div span a p 等标签,移动端的标签为class,可以通过appium查看
Appium的安装和手机互联
安装:傻瓜式安装 手机互联
- 启动appium
- usb连接手机,并要求手机设置为开发者模式,文件传输模式,个别手机还需要在开发者选项中勾选一定内容(如果连接失败可以考虑搜索特定机型的勾选内容)
- 常见问题总结
微信朋友圈抓取
from appium import webdriver
import time
from appium.webdriver.extensions.android.nativekey import AndroidKey
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def main():
desired_caps = {
'platformName': 'Android',
'platformVersion': '11',
'deviceName': 'xxx',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'unicodeKeyboard': True,
'resetKeyboard': True,
'noReset': True,
'newCommandTimeout': 6000,
'automationName': 'UiAutomator2'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
search = driver.find_element_by_xpath("//*[@text='发现']//preceding-sibling::android.widget.RelativeLayout//android.widget.ImageView")
search.click()
driver.implicitly_wait(5)
friendGroup = driver.find_element_by_xpath("//android.widget.ListView//android.widget.LinearLayout[@resource-id='com.tencent.mm:id/h8z'][1]")
friendGroup.click()
driver.implicitly_wait(5)
size = driver.get_window_size()
height = size["height"]
count = 0
item_list = []
for it in range(10):
items = driver.find_elements_by_xpath('//*[@resource-id="com.tencent.mm:id/hyd"]')
for item in items:
try:
nickname = item.find_element_by_xpath('//*[@resource-id="com.tencent.mm:id/fzg"]').get_attribute(
'text')
content = item.find_element_by_xpath('//*[@resource-id="com.tencent.mm:id/bmy"]').get_attribute(
'text')
if nickname and content:
count = count + 1
item_list.append(nickname)
item_list.append(content)
else:
print("")
except:
pass
driver.swipe(300, 1000, 300, 300)
time.sleep(2)
final_list = []
for it in item_list:
if it not in final_list:
final_list.append(it)
print("去重后:")
for it in item_list:
print(it)
if __name__ == '__main__':
main()
|