Python selenium抓取虎牙短視頻代碼實例
更新時間:2020年03月02日 14:48:53 作者:pfeiliu
這篇文章主要介紹了Python selenium抓取虎牙短視頻代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
今天閑著沒事,用selenium抓取視頻保存到本地,只爬取了第一頁,只要小于等于5分鐘的視頻。。。
為什么不用requests,沒有為什么,就因為有些網站正則和xpath都提取不出來想要的東西,要么就是接口出來的數(shù)據(jù)加密,要么就因為真正的視頻url規(guī)律難找!
selenium幾行代碼輕輕松松就搞定!
安裝selenium庫,設置無界面模式
代碼如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options import requests #設置無界面模式 chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') class VideoCrawl(object): video_box=[]#收集video真正的url def __init__(self,url): self.driver=webdriver.Chrome(executable_path=r"C:\Program Files\python\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe",options=chrome_options)#設置無界面模式 self.driver.get(url) #程序運行完畢,析構函數(shù)關閉selenium def __del__(self): print("爬取結束。。。。。",len(VideoCrawl.video_box),VideoCrawl.video_box) self.driver.close() def run(self): self.get_detail_info() #獲取列表頁所有詳情頁的url def get_detail_info(self): detail_info = self.driver.find_elements_by_xpath('//a[@class="video-wrap statpid"]') detail_url=[] for i in detail_info: detail_url.append(i.get_attribute('href'))#獲取視頻頁url video_playtime_list=self.driver.find_elements_by_xpath('//span[@class="video-duration"]') video_playtime_list=[i.text for i in video_playtime_list] for res in zip(detail_url,video_playtime_list): playtime=res[1].split(":")[0] # print("playtime--------",playtime) if int(res[1].split(":")[0])<=5:#播放時間小于5分鐘的要 # print(res[0],"解析的url",playtime) self.parse_video(res[0],res[1]) else: pass #解析詳情頁 def parse_video(self,url,t): self.driver.get(url) videoobj = self.driver.find_elements_by_xpath('//video') video_url=videoobj[0].get_attribute('src') title=self.driver.find_elements_by_xpath('//h1[@class="video-title"]')[0].text print('video_url--------',video_url,title,t) #保存video到本地 self.save_video(video_url,title,t) #類變量統(tǒng)計video_url VideoCrawl.video_box.append(video_url) #保存,請求video_url,二進制保存為mp4 def save_video(self,url,title,t): filename="video"+title+"-"+t.replace(":","")+".mp4" video=requests.get(url).content with open(filename,"wb") as file: file.write(video) print(f"{filename}寫入文件完畢") if __name__ == '__main__': crawl=VideoCrawl('https://v.huya.com/cat/7') crawl.run()
運行結果如下:
"C:\Program Files\python\python.exe" C:/Users/Administrator.SC-201903160419/Desktop/note/exer/myapp.py video_url-------- https://huya-w10.huya.com/2005/265917310/1300/d973823b0f437c9d78fc40b9691fdb54.mp4 【軒子小劇場】最意外的自行車 04:23 video【軒子小劇場】最意外的自行車-0423.mp4寫入文件完畢 video_url-------- https://huya-w10.huya.com/2006/267302224/1300/f8a363ec243e4adb2857491f695bc118.mp4 軒子巨2兔:軒子教你演戲 05:06 video軒子巨2兔:軒子教你演戲-0506.mp4寫入文件完畢 video_url-------- https://huya-w6.huya.com/2005/264805062/1300/582b726b05db31fc12a1e5557011a6bf.mp4 【麥秀彩兒】跳個舞吧 05:58 video【麥秀彩兒】跳個舞吧-0558.mp4寫入文件完畢 video_url-------- https://huya-w10.huya.com/2005/264956230/1300/97fa603f7b174ec30c19013f894bd108.mp4 軒子小劇場:你的女仆請簽收 01:18 Process finished with exit code -1
都可以正常播放。。。
切記:自己娛樂下練練手刪了即可,千萬不要用于商業(yè)用途哦!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python?matplotlib數(shù)據(jù)可視化圖繪制
這篇文章主要介紹了Python?matplotlib數(shù)據(jù)可視化圖繪制,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07