Python爬蟲入門 | 7 分類爬取豆瓣電影,解決動態載入問題
![8134750-261ecfac218f10a7.png](https://i.iter01.com/images/6cc7a026e80e287feee02aa4b3c64d70017173f3a43f2866a4d2fe5eef8efc5f.png)
![8134750-6e20f7398da33aac.png](https://i.iter01.com/images/75e748bada292ace8a8d05fb13bcfa38c6e7d791e8d9689e1cd86068d3d5780a.png)
![8134750-bfa9b8bb7d7a4059.png](https://i.iter01.com/images/b115bea4886fbfe593902b349902955e1fee8687c0cefd055b3ec1924cc5064f.png)
比如我們今天的案例,豆瓣電影分類頁面。根本沒有什麼翻頁,需要點選“載入更多”新的電影資訊,前面的黑科技瞬間被秒……
![8134750-c5962af1621f526b.png](https://i.iter01.com/images/2d22bfe1f43872c13a28c3bd59416a07ff08a0abef38c4d028d3acc8cfec5394.png)
又比如知乎關注的人列表頁面:
![8134750-4fd4765ee4b3d5e3.png](https://i.iter01.com/images/3f5875b2bbb8bbde11389cd85976019e003f0450060c11f22af31b339d273a9c.png)
我複製了其中兩個人暱稱的 xpath:
//*[@id="Popover-35130-11238-toggle"]/a
//*[@id="Popover-35130-42416-toggle"]/a
竟然需要 ID 這種噁心的東西,規律失效。
![8134750-f8af02bda22b7517.png](https://i.iter01.com/images/7a03b33c0ba07c6e2b1060528164cf0a3d589c75d50fd6ef0eca7ebc9df44bb3.png)
![8134750-ce7f63dac69c4dc8.png](https://i.iter01.com/images/93605a95a78268f25896ab668c512ab6365ae433c2369deaf8e082bac7ab4806.png)
我們以豆瓣勵志分類下的電影為例,連結在這裡:
https://movie.douban.com/tag/#/?sort=T&range=0,10&tags=%E5%8A%B1%E5%BF%97
![8134750-cbd9980e45bf072f.png](https://i.iter01.com/images/e95ab49e6fd5d668daeaab1a96dd22d5a45e89162dc39aad2780f26889ded354.png)
![8134750-8a7e15d31f355c66.png](https://i.iter01.com/images/93423f95a00790873195272b78559a9f3d01aa543582bdd59201492e63c7c74e.png)
上面的標記應該是“分類”,而不是“排行榜”,更正一下。
首先要告訴你的是,這種動態載入的頁面,一般資料會在Network的 JS或者 XHR 類目裡。
![8134750-b36cf529a691f62f.png](https://i.iter01.com/images/056b5961e762afe080000b75bdc69d298860afda297a47ef1751664660ee85ff.png)
我們開啟開發者工具,看這一頁的 XHR裡沒有任何檔案,然後點選載入更多按鈕,看它給我們返回什麼資訊。
![8134750-3be8c31b51c21e24.png](https://i.iter01.com/images/9bdaed47ea4088e466df2cdf4c55c86a0340848cc72bfe371e4c5b9fec184bb2.png)
咦?返回了一個新的檔案,出於好奇,我們有必要看看究竟
右鍵點選 >> Open in new tab
有的網站 返回的 JS、XHR 資訊比較多,需要你去嘗試和篩選。
![8134750-d8a298cde8b0e998.png](https://i.iter01.com/images/7cf9b01e1fdc98cbbcf64457ed57042631fcc1e1e891ba11a79bd85697470475.png)
這個 json 頁面看起來就很親切了,包含電影名稱、導演、評分、演員、連結等資料。最關鍵的是,我仔細看了一遍頁面後發現,這一頁的電影資訊,正好是新載入出來的所有電影的資訊。
你的網頁看 json 很亂?不要著急,那是你的chrome沒有安裝jsonview這個外掛。證號為你準備好了,子按下方的雲盤下載、安裝就好:
連結:http://pan.baidu.com/s/1nvefj0t 密碼:13pm
![8134750-ff8d9bb92cadfe9a.png](https://i.iter01.com/images/ffa78c37bb8fd5ab30756aecd7514137452186f31e0d87035854e2f2f34fea1d.png)
好了,又載入了兩次,不出意料地又載入出了兩頁 XHR 資訊,於是,滿懷期待地分別開啟它們。新載入的兩個頁面,和網頁顯示的電影資訊完全相同。
![8134750-f455262c3f91234c.png](https://i.iter01.com/images/d4f53431950401df3295ebca0848cfe10caba3365ad8ec70666dd2acebd15ee5.png)
![8134750-071aff3a1dd08750.png](https://i.iter01.com/images/689fae87b93042b26dbc6d7f79237d16b10e0ec6be3e17855396dfdb362339ba.png)
我們再來看看 XHR 載入的這幾個頁面的 url:
#第二頁
https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=20
#第三頁
https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=40
#第四頁
https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=60
比較後就可以輕鬆發現,這些頁面的 url 是有規律的:只有最後 start= 後面的數字在變化,而且是以20為步長遞增的,20正好對應每次載入出來的電影數量。
於是我們可以輕鬆地寫出頁面迴圈來爬取:
for a in range(3):
url='https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start={}'.format(a*20)
# 用 a*20 表示每個頁面按 20 的步長遞增,只示例3個頁面,你可以按需求增加。
按照前面的套路寫出程式碼,並得到結果:
import requests
import json
import time
for a in range(3):
url_visit = 'https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start={}'.format(a*20)
file = requests.get(url_visit).json() #這裡跟之前的不一樣,因為返回的是 json 檔案
time.sleep(2)
for i in range(20):
dict=file['data'][i] #取出字典中 'data' 下第 [i] 部電影的資訊
urlname=dict['url']
title=dict['title']
rate=dict['rate']
cast=dict['casts']
print('{} {} {} {}\n'.format(title,rate,' '.join(cast),urlname))
爬取的資料如下:
![8134750-3c6287ef6744e7ae.png](https://i.iter01.com/images/277d4c427a2ba780547b28abcd3b65f631566113ff07e3fce7b19570f6030b98.png)
解釋一下程式碼中的一些細節:
file = requests.get(url).json()
之前我們用的 .text 是需要網頁返回文字的資訊,而這裡返回的是 json檔案所以用 .json()
dict=file['data'][i]
urlname=dict['url']
取出字典中的值,需要在方括號中指明值對應的鍵
' '.join(cast)
因為有多名演員,這裡用了 join() 函式,在字串中間加入空格分隔。
當然,你也可以把爬下來的資訊存到本地:
![8134750-a01c305e19113cd1.png](https://i.iter01.com/images/d2f9c2cc3898527797705012846d57c76d7d61bb15b05b75751dc69b85b46227.png)
對電影評分進行排序,不久得到了所有的高分電影嗎?
![8134750-8116f1eb351de645.png](https://i.iter01.com/images/026da24974565dbaea7d8bda34a97a535975c98c538d0670b80eca07758723e6.png)
![8134750-d2e47e2d61f27e93.png](https://i.iter01.com/images/423d00777ba842e7c8fa6c6eccb33ed185fe7a8109b2b34fa14b1b30acbcbebe.png)
![8134750-1210f33a7d109952.png](https://i.iter01.com/images/014de389e19c6a3ca0afbc5c6ed46c4eb44302622abecc607da45ec6bcc3620f.png)
完整7節課程目錄:
Python爬蟲入門 | 1 Python環境的安裝
Python爬蟲入門 | 2 爬取豆瓣電影資訊
Python爬蟲入門 | 3 爬蟲必備Python知識
Python爬蟲入門 | 4 爬取豆瓣TOP250圖書資訊
Python爬蟲入門 | 5 爬取小豬短租租房資訊
Python爬蟲入門 | 6 將爬回來的資料存到本地
Python爬蟲入門 | 7 分類爬取豆瓣電影,解決動態載入問題
白白~
相關文章
- scrapy入門:豆瓣電影top250爬取
- Python爬蟲筆記(4):利用scrapy爬取豆瓣電影250Python爬蟲筆記
- Python爬蟲教程-17-ajax爬取例項(豆瓣電影)Python爬蟲
- python爬蟲 爬取豆瓣電影 1-10 ajax 資料Python爬蟲
- 爬蟲01:爬取豆瓣電影TOP 250基本資訊爬蟲
- Python爬蟲入門【5】:27270圖片爬取Python爬蟲
- Python爬蟲入門Python爬蟲
- Python爬蟲入門【10】:電子書多執行緒爬取Python爬蟲執行緒
- 【python爬蟲案例】利用python爬取豆瓣電影TOP250評分排行資料!Python爬蟲
- Python爬取分析豆瓣電影Top250Python
- 使用python爬取豆瓣電影TOP250Python
- Python爬蟲入門教程 50-100 Python3爬蟲爬取VIP視訊-Python爬蟲6操作Python爬蟲
- python-爬蟲入門Python爬蟲
- python爬蟲爬取網頁中文亂碼問題的解決Python爬蟲網頁
- Python爬蟲入門【9】:圖蟲網多執行緒爬取Python爬蟲執行緒
- Python爬蟲入門【11】:半次元COS圖爬取Python爬蟲
- Python爬蟲入門【3】:美空網資料爬取Python爬蟲
- Python爬蟲入門【4】:美空網未登入圖片爬取Python爬蟲
- python-爬蟲-css提取-寫入csv-爬取貓眼電影榜單Python爬蟲CSS
- 【爬蟲】python爬蟲從入門到放棄爬蟲Python
- python網路爬蟲(7)爬取靜態資料詳解Python爬蟲
- 用python寫一個豆瓣短評通用爬蟲(登入、爬取、視覺化)Python爬蟲視覺化
- scrapy爬取豆瓣電影資料
- python更換代理爬取豆瓣電影資料Python
- 專案之爬蟲入門(豆瓣TOP250)爬蟲
- 什麼是Python爬蟲?python爬蟲入門難嗎?Python爬蟲
- Python《成功破解簡單的 動態載入 的爬蟲》Python爬蟲
- 爬蟲入門基礎-Python爬蟲Python
- python3 爬蟲入門Python爬蟲
- 爬蟲入門爬蟲
- 騰訊動漫爬蟲與動態隨機載入反爬爬蟲隨機
- Python爬蟲入門【7】: 蜂鳥網圖片爬取之二Python爬蟲
- Python爬蟲入門教程 2-100 妹子圖網站爬取Python爬蟲網站
- 三分鐘爬蟲入門爬蟲
- Python 從零開始爬蟲(六)——動態爬取解決方案 之 手動分析Python爬蟲
- Python爬蟲入門,8個常用爬蟲技巧盤點Python爬蟲
- Python爬蟲例項:爬取貓眼電影——破解字型反爬Python爬蟲
- python爬蟲 之 BeautifulSoup庫入門Python爬蟲
- Python3爬蟲入門(一)Python爬蟲