1. 程式人生 > >Python3+Fiddler爬取手機端APP(三) ————使用Appium在真機模擬爬取

Python3+Fiddler爬取手機端APP(三) ————使用Appium在真機模擬爬取

最終:

在這裡插入圖片描述

例子: 爬取天眼查: 1.手機開啟開發者模式,允許USB除錯 2.cmd輸入adb 在這裡插入圖片描述 3.下載apk 4.開啟appium,拖入apk,並填上裝置名 在這裡插入圖片描述 5.寫對應python程式

import selenium
import time
from appium import webdriver

class MyTestApp(object):
    def setUp(self):
        print('selenium version = ', selenium.__version__)
        desired_caps = {}
        desired_caps[
'platformName'] = 'Android' desired_caps['platformVersion'] = '21' desired_caps['deviceName'] = '5JPDU17911000775' desired_caps['appPackage'] = 'com.tianyancha.skyeye' desired_caps['appActivity'] = 'com.tianyancha.skyeye.SplashPage'
desired_caps['app'] = 'C://Users/Administrator/Desktop/test.apk' desired_caps["unicodeKeyboard"] ="True" desired_caps["resetKeyboard"] ="True" self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

6.啟動appium後啟動程式,手機即可自動開啟天眼查APP 在這裡插入圖片描述 7.天眼查APP開啟需向左滑動三次並點選立即體驗,才能進入APP,開始搜尋,使用uiautomatorviewer定位元素。 在這裡插入圖片描述

在這裡插入圖片描述 8.進入後找到輸入框輸入公司名字即可,程式如下:


import selenium
import time
from appium import webdriver

class MyTestApp(object):


    def setUp(self):

        print('selenium version = ', selenium.__version__)
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '21'
        desired_caps['deviceName'] = '5JPDU17911000775'                   #127.0.0.1:62001
        desired_caps['appPackage'] = 'com.tianyancha.skyeye'                                  
        desired_caps['appActivity'] = 'com.tianyancha.skyeye.SplashPage'                       #com.tianyancha.skyeye.SplashPage
        desired_caps['app'] = 'C://Users/Administrator/Desktop/test.apk'
        desired_caps["unicodeKeyboard"] ="True"
        desired_caps["resetKeyboard"] ="True"

        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        time.sleep(5)
        for i in range(3):
            print(i)
            self.driver.swipe(900,900,100,900,duration=2000)
            time.sleep(3)
        self.driver.find_element_by_id('com.tianyancha.skyeye:id/launch_btn').click()
        time.sleep(2)
        self.driver.find_element_by_id('com.tianyancha.skyeye:id/txt_search_copy1').click()
        time.sleep(2)
        for i in range(10):
            self.driver.find_element_by_id('com.tianyancha.skyeye:id/search_input_et').send_keys(u'百度網訊')
            time.sleep(2)
            self.driver.find_element_by_id('com.tianyancha.skyeye:id/search_clean_iv').click()
            # self.driver.find_element_by_id('com.tianyancha.skyeye:id/search_input_et').clear()
            # self.driver.tap([(50,135)])

if __name__ == '__main__':
    mt = MyTestApp()
    mt.setUp()

9.可以使用fiddler的Fiddler ScriptEditor對攔截的包進行儲存獲得資料 在這裡插入圖片描述