本篇文章給大家分享的是有關(guān)Python怎么爬取當(dāng)當(dāng)網(wǎng)APP數(shù)據(jù),小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)建站公司2013年成立,先為梅里斯等服務(wù)建站,梅里斯等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為梅里斯企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
場景:有時候通過傳統(tǒng)的方法去爬一些 Web 網(wǎng)頁或者 APP,受限于對方的反爬方案,很難爬到想要的數(shù)據(jù),這個時候可以考慮使用「Appium」結(jié)合「mitmproxy」的方式去爬取數(shù)據(jù)。
其中,Appium 負(fù)責(zé)驅(qū)動 App 端自動化運行,mitmproxy 負(fù)責(zé)截取請求數(shù)據(jù)并解析保存到數(shù)據(jù)庫。
今天的目的是爬取「當(dāng)當(dāng)網(wǎng)」的所有數(shù)據(jù),并保存到 MongoDB 數(shù)據(jù)庫當(dāng)中。
首先,需要在 PC 上安裝好 Charles 和 Appium Desktop,并配置好 mitmproxy 環(huán)境。
# 安裝mitmproxy依賴包 pip3 install mitmproxy # 安裝pymongodb pip3 install pymongo
另外,需要準(zhǔn)備一臺 Android 手機,另外 PC 端配置好 Android 開發(fā)環(huán)境。
1. 在配置好手動代理的情況下,打開 Charles 實時捕獲客戶端的發(fā)起的網(wǎng)絡(luò)請求。
打開當(dāng)當(dāng)網(wǎng)搜索商品的頁面,搜索關(guān)鍵字「Python」,可以在 Charles 查看到當(dāng)前請求的 URL 地址包含:「word=Python」
編寫 mitmproxy 的執(zhí)行腳本文件,重寫 response() 函數(shù),通過對請求的 URL 進行過濾,對有用的數(shù)據(jù)進行整理并保存到 MongoDB 數(shù)據(jù)庫當(dāng)中。
class DangDangMongo(object): """ 初始化MongoDB數(shù)據(jù)庫 """ def __init__(self): self.client = MongoClient('localhost') self.db = self.client['admin'] self.db.authenticate("root", "xag") self.dangdang_book_collection = self.db['dangdang_book'] def response(flow): # 過濾請求的URL if 'keyword=Python' in request.url: data = json.loads(response.text.encode('utf-8')) # 書籍 products = data.get('products') or None product_datas = [] for product in products: # 書ID product_id = product.get('id') # 書名 product_name = product.get('name') # 書價格 product_price = product.get('price') # 作者 authorname = product.get('authorname') # 出版社 publisher = product.get('publisher') product_datas.append({ 'product_id': product_id, 'product_name': product_name, 'product_price': product_price, 'authorname': authorname, 'publisher': publisher }) DangDangMongo().dangdang_book_collection.insert_many(product_datas) print('成功插入數(shù)據(jù)成功')
先打開客戶端的手動代理監(jiān)聽 8080 端口,然后執(zhí)行「mitmdump」命令,然后滾動商品界面,發(fā)現(xiàn)數(shù)據(jù)到寫入到數(shù)據(jù)庫中了。
mitmdump -s script_dangdang.py
2. 下面我們要利用 Appium 幫我們實現(xiàn) 自動化。
首先打開 Appium Desktop,并啟動服務(wù)。
獲取到包名和初始 Activity 后,就可以利用 WebDriver 去模擬打開當(dāng)當(dāng)網(wǎng) APP。
self.caps = { 'automationName': DRIVER, 'platformName': PLATFORM, 'deviceName': DEVICE_NAME, 'appPackage': APP_PACKAGE, 'appActivity': APP_ACTIVITY, 'platformVersion': ANDROID_VERSION, 'autoGrantPermissions': AUTO_GRANT_PERMISSIONS, 'unicodeKeyboard': True, 'resetKeyboard': True } self.driver = webdriver.Remote(DRIVER_SERVER, self.caps)
接著使用 Android SDK 自帶的工具 uiautomatorviewer 獲取到元素信息,使用 Appium 中的 WebDriver 去操作 UI 元素。
第一次打開應(yīng)用的時候,可能會出現(xiàn)紅包雨對話框、新人專享紅包對話框、切換城市對話框,這里需要通過元素 ID 獲取到關(guān)閉按鈕,執(zhí)行點擊操作來關(guān)閉這些對話框。
這里創(chuàng)建一個 新的線程 來單獨處理這些對話框。
class ExtraJob(threading.Thread): def run(self): while self.__running.isSet(): # 為True時立即返回, 為False時阻塞直到內(nèi)部的標(biāo)識位為True后返回 self.__flag.wait() # 1.0 【紅包雨】對話框 red_packet_element = is_element_exist(self.driver, 'com.dangdang.buy2:id/close') if red_packet_element: red_packet_element.click() # 1.1 【新人專享券】對話框 new_welcome_page_sure_element = is_element_exist(self.driver, 'com.dangdang.buy2:id/dialog_cancel_tv') if new_welcome_page_sure_element: new_welcome_page_sure_element.click() # 1.2 【切換位置】對話框 change_city_cancle_element = is_element_exist(self.driver, 'com.dangdang.buy2:id/left_bt') if change_city_cancle_element: change_city_cancle_element.click() extra_job = ExtraJob(dangdang.driver) extra_job.start()
接下來就是點擊搜索按鈕,然后輸入內(nèi)容,執(zhí)行點擊搜索對話框。
# 1.搜索框 search_element_pro = self.wait.until( EC.presence_of_element_located((By.ID, 'com.dangdang.buy2:id/index_search'))) search_element_pro.click() search_input_element = self.wait.until( EC.presence_of_element_located((By.ID, 'com.dangdang.buy2:id/search_text_layout'))) search_input_element.set_text(KEY_WORD) # 2.搜索對話框,開始檢索 search_btn_element = self.wait.until( EC.element_to_be_clickable((By.ID, 'com.dangdang.buy2:id/search_btn_search'))) search_btn_element.click() # 3.休眠3秒,保證第一頁的內(nèi)容加載完全 time.sleep(3)
待第一頁的數(shù)據(jù)加載完全之后,可以一直向上滾動頁面,直到數(shù)據(jù)全部被加載完全,數(shù)據(jù)會由 mitmproxy 自動保存到 MongoDB 數(shù)據(jù)庫當(dāng)中。
while True: str1 = self.driver.page_source self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_X) time.sleep(1) str2 = self.driver.page_source if str1 == str2: print('停止滑動') # 停止線程 extra_job.stop() break print('繼續(xù)滑動'
首先使用 mitmdump 開啟請求監(jiān)聽的服務(wù),然后執(zhí)行爬取腳本。
App 會自動打開,執(zhí)行一系列操作后,到達商品界面,然后自動滑動界面,通過 mitmproxy 自動把有用的數(shù)據(jù)保存到 MongoDB 數(shù)據(jù)庫中。
以上就是Python怎么爬取當(dāng)當(dāng)網(wǎng)APP數(shù)據(jù),小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:Python怎么爬取當(dāng)當(dāng)網(wǎng)APP數(shù)據(jù)
本文地址:http://muchs.cn/article34/ijohpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器、手機網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、App設(shè)計、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)