您好,登錄后才能下訂單哦!
小編給大家分享一下Python中selenium如何抓取虎牙短視頻,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
安裝selenium庫,設(shè)置無界面模式
代碼如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options import requests #設(shè)置無界面模式 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)#設(shè)置無界面模式 self.driver.get(url) #程序運(yùn)行完畢,析構(gòu)函數(shù)關(guān)閉selenium def __del__(self): print("爬取結(jié)束。。。。。",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:#播放時(shí)間小于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('//h2[@class="video-title"]')[0].text print('video_url--------',video_url,title,t) #保存video到本地 self.save_video(video_url,title,t) #類變量統(tǒng)計(jì)video_url VideoCrawl.video_box.append(video_url) #保存,請(qǐng)求video_url,二進(jìn)制保存為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()
運(yùn)行結(jié)果如下:
"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 【麥秀彩兒】跳個(gè)舞吧 05:58 video【麥秀彩兒】跳個(gè)舞吧-0558.mp4寫入文件完畢 video_url-------- https://huya-w10.huya.com/2005/264956230/1300/97fa603f7b174ec30c19013f894bd108.mp4 軒子小劇場:你的女仆請(qǐng)簽收 01:18 Process finished with exit code -1
都可以正常播放。。。
切記:自己娛樂下練練手刪了即可,千萬不要用于商業(yè)用途哦!
以上是“Python中selenium如何抓取虎牙短視頻”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。