如何使用appnium爬取视频数据
要先安装Appium,并用它来查找对应元素的id、xpath等信息
from appium import webdriver from appium.webdriver.extensions.android.nativekey import AndroidKey from selenium.webdriver.common.by import By import time # 注意要在程序运行前打开Appium,否则程序会报错:ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。 DesireCaps = { "platformName": "Android", "platformVersion": "11", # 手机安卓版本 "deviceName": "MyLaptop", "appPackage": "tv.danmaku.bili", # appPackage名称 # 查看应用包名: # 1、没有apk # 在手机上进入自己要操作的页面,然后在命令行窗口执行:adb shell dumpsys activity recents | find "intent={" # 就会产生:cmp=xxx/yyy……,xxx即为appPackage,yyy即为appActivity(只看第一行,下面的可能为其他包名),一般是:intent={……cmp=xxx/yyy……} # 2、有apk # aapt.exe的路径 dump badging 软件apk的路径 | find "package: name="(我这里的aapt.exe的路径为:D:Appiumappiumandroidsdkuild-tools29.0.3aapt.exe) # aapt.exe的路径 dump badging 软件apk的路径 | find "launchable-activity" "appActivity": ".MainActivityV2", # appActivity名称(启动界面) # 或者写成appPackage.appActivity "unicodeKeyboard": True, # 使用自带输入法,需要输入除英文字符以外的字符 "resetKeyboard": True, # 执行完程序后恢复原来输入法 "noReset": True, "newCommandTimeout": 6000, "automationName": "UiAutomator2" } driver = webdriver.Remote("http://localhost:4723/wd/hub", DesireCaps) # 第一次运行的时候手机会自动安装自动化代理 # 可根据id、ClassName、Accessibility、Xpath来定位元素 time.sleep(3) driver.find_element(By.XPATH, "//android.widget.TextView[@content-desc="搜索"]").click() # 注意要importBy time.sleep(3) driver.find_element(By.XPATH, "//android.widget.EditText[@content-desc="搜索查询"]").send_keys("onepiece") # 后面不能直接加.press_keycode(AndroidKey.ENTER)。而是要另起一行 driver.press_keycode(AndroidKey.ENTER) # 输入回车 time.sleep(3) TextView1s = driver.find_elements(By.XPATH, """/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.TextView[1]""") TextView2s = driver.find_elements(By.XPATH, """/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.TextView[2]""") TextView3s = driver.find_elements(By.XPATH, """/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.TextView[3]""") TextView4s = driver.find_elements(By.XPATH, """/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.TextView[4]""") TextView5s = driver.find_elements(By.XPATH, """/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.view.ViewGroup/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup/android.widget.TextView[5]""") for i in list(range(5)): print(TextView1s[i].text) print(TextView2s[i].text) print(TextView3s[i].text) print(TextView4s[i].text) print(TextView5s[i].text)