最近被室友安利熱血動漫番《終末的女武神》和《拳願阿修羅》,太上頭了週末休息熬夜看完了。不過資源不太好找,辣條一怒爬取了資源,這下可以看個夠了。室友崇拜連連,想起了我的班花,快點開學啊,阿西吧...

Python爬蟲-vip動漫採集

效果展示

爬取目標

網站目標:櫻花動漫

工具使用

開發工具:pycharm

開發環境:python3.7, Windows10

使用工具包:requests,lxml, re,tqdm

重點學習內容

正則的使用 tqdm的使用 各種音訊資料的處理

專案思路解析

搜尋你需要的動漫資料,根據自己需要的視訊不同解析視訊的方法也是不一樣的(會挑選兩種視訊進行解析)

在當前頁面需要提取出對應的章節資訊,獲取到章節資訊的a標籤的跳轉內容,提取出每個章節的名字,提取章節的方法我使用的xpath的方法(各位大佬可自行嘗試其他的方法)

  1. headers = {
  2.    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
  3.    'Referer': 'http://www.imomoe.la/search.asp'
  4. }

  5. url = 'http://www.imomoe.la/view/8024.html'
  6. response = requests.get(url, headers=headers)
  7. # print(response.content.decode('gbk'))
  8. html_data = etree.HTML(response.content.decode('gbk'))
  9. chapter_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/text()')
  10. chapter_url_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/@href')[0]

url的資料需要自行拼接,根據新的url獲取詳情頁面的資料

按照正常思路首先應該檢視播放地址是否為靜態資料

明顯看出資料並不是靜態資料,在區分是否為動態資料,通過抓包工具進行獲取。

也並不是動態資料,媒體資料也不知道怎麼形成的。

從頭在來從前端頁面在進行解析,找視訊頁面的事件。

並沒有發現有效資料,但是在iframe下面的Script標籤有js跳轉地址 ,解析的資料網址和視訊的播放地址是一樣的域名, 點選檢視, 這不是就是我們找的視訊播放地址嘛 ,終於找到了,開始實現 在當前頁面通過xpath方式提取出script裡的js跳轉地址, 拼接出新的視訊連結播放地址,傳送請求,通過正則表示式提取出所有MP4播放地址。

  1. new_url = 'http://www.imomoe.la' + chapter_url_list
  2. response = requests.get(new_url, headers=headers)
  3. html = etree.HTML(response.content.decode('gbk'))

  4. data_url = 'http://www.imomoe.la' + html.xpath('//div[@class="player"]/script[1]/@src')[0]
  5. res = requests.get(data_url, headers=headers).text
  6. # print(res)
  7. play_url_list = re.findall('\$(.*?)\$flv', res)
  8. print(play_url_list)

儲存對視訊資料傳送請求,儲存資料到mp4 ,通過tqdm工具能檢視對應下載的速度以及下載的進度

  1. for chapter, play_url in tqdm(zip(chapter_list, play_url_list)):
  2.   result = requests.get(play_url, headers=headers).content
  3.   f = open('終末的女武神/' + chapter + '.mp4', "wb")
  4.   f.write(result)

  1. ​到這大功告成 但是當我把網址修改成鬥破蒼穹這個動漫時,卻返回的資料為空

這個視訊的載入資料的規則是不一樣的載入的資料為m3u8的格式, 其他的音訊的資料載入可能也不一樣, 處理m3u8的資料稍稍的有丟丟複雜,它的m3u8的檔案內部有嵌套了m3u8連結地址, 需要轉換對應的資料介面,進行連結地址拼接, 取出ts檔案進行下載,拼接成視訊。

  1. m3u8_url_list = re.findall('\$(.*?)\$bdhd', res)
  2. for m3u8_url, chapter in zip(m3u8_url_list, chapter_list):
  3.    data = requests.get(m3u8_url, headers=headers)
  4.    # print(data.text)
  5.    new_m3u8_url = 'https://cdn.605-zy.com/' + re.findall('/(.*?m3u8)', data.text)[0]
  6.    # print(new_m3u8_url)
  7.    ts_data = requests.get(new_m3u8_url, headers=headers)
  8.    ts_url_list = re.findall('/(.*?ts)', ts_data.text)
  9.    print("正在下載:", chapter)
  10.    for ts_url in tqdm(ts_url_list):
  11.        result = requests.get('https://cdn.605-zy.com/' + ts_url).content
  12.        f = open('鬥破蒼穹/' + chapter + '.mp4', "ab")
  13.        f.write(result)

專案思路總結

  • 獲取到想要動漫的地址

  • 提取詳情頁面的名字已經跳轉地址

  • 獲取頁面的靜態js檔案

  • 解析視訊播放地址或者m3u8檔案

  • 儲存對應資料

簡易原始碼分享

  1. import requests
  2. from lxml import etree
  3. import re
  4. from tqdm import tqdm


  5. headers = {
  6.    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
  7.    'Referer': 'http://www.imomoe.la/search.asp'
  8. }

  9. url = 'http://www.imomoe.la/view/8024.html'
  10. response = requests.get(url, headers=headers)
  11. # print(response.content.decode('gbk'))
  12. html_data = etree.HTML(response.content.decode('gbk'))
  13. chapter_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/text()')
  14. chapter_url_list = html_data.xpath('//div[@class="movurl"]/ul/li/a/@href')[0]
  15. # print(chapter_list)
  16. # print(chapter_url_list)
  17. new_url = 'http://www.imomoe.la' + chapter_url_list
  18. response = requests.get(new_url, headers=headers)
  19. html = etree.HTML(response.content.decode('gbk'))

  20. data_url = 'http://www.imomoe.la' + html.xpath('//div[@class="player"]/script[1]/@src')[0]
  21. res = requests.get(data_url, headers=headers).text
  22. # print(res)
  23. play_url_list = re.findall('\$(.*?)\$flv', res)
  24. print(play_url_list)

  25. for chapter, play_url in tqdm(zip(chapter_list, play_url_list)):
  26.    result = requests.get(play_url, headers=headers).content
  27.    f = open('終末的女武神/' + chapter + '.mp4', "wb")
  28.    f.write(result)

  1.  



發現不會的或者學習Python的,可以直接評論留言或者私我【非常感謝你的點贊、收藏、關注、評論,一鍵四連支援】