Python selenium抓取虎牙短視頻代碼實(shí)例
今天閑著沒事,用selenium抓取視頻保存到本地,只爬取了第一頁(yè),只要小于等于5分鐘的視頻。。。
為什么不用requests,沒有為什么,就因?yàn)橛行┚W(wǎng)站正則和xpath都提取不出來(lái)想要的東西,要么就是接口出來(lái)的數(shù)據(jù)加密,要么就因?yàn)檎嬲囊曨lurl規(guī)律難找!
selenium幾行代碼輕輕松松就搞定!
安裝selenium庫(kù),設(shè)置無(wú)界面模式
代碼如下:
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsimport requests#設(shè)置無(wú)界面模式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 FilespythonLibsite-packagesseleniumwebdriverchromechromedriver.exe',options=chrome_options)#設(shè)置無(wú)界面模式 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() #獲取列表頁(yè)所有詳情頁(yè)的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’))#獲取視頻頁(yè)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 #解析詳情頁(yè) 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)計(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 Filespythonpython.exe' C:/Users/Administrator.SC-201903160419/Desktop/note/exer/myapp.pyvideo_url-------- https://huya-w10.huya.com/2005/265917310/1300/d973823b0f437c9d78fc40b9691fdb54.mp4 【軒子小劇場(chǎng)】最意外的自行車 04:23video【軒子小劇場(chǎng)】最意外的自行車-0423.mp4寫入文件完畢video_url-------- https://huya-w10.huya.com/2006/267302224/1300/f8a363ec243e4adb2857491f695bc118.mp4 軒子巨2兔:軒子教你演戲 05:06video軒子巨2兔:軒子教你演戲-0506.mp4寫入文件完畢video_url-------- https://huya-w6.huya.com/2005/264805062/1300/582b726b05db31fc12a1e5557011a6bf.mp4 【麥秀彩兒】跳個(gè)舞吧 05:58video【麥秀彩兒】跳個(gè)舞吧-0558.mp4寫入文件完畢video_url-------- https://huya-w10.huya.com/2005/264956230/1300/97fa603f7b174ec30c19013f894bd108.mp4 軒子小劇場(chǎng):你的女仆請(qǐng)簽收 01:18 Process finished with exit code -1
都可以正常播放。。。
切記:自己娛樂(lè)下練練手刪了即可,千萬(wàn)不要用于商業(yè)用途哦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. Python matplotlib 繪制雙Y軸曲線圖的示例代碼3. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除4. java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作5. 詳解JSP 內(nèi)置對(duì)象request常見用法6. jsp EL表達(dá)式詳解7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)
