Python selenium抓取虎牙短视频代码实例
今天闲着没事,用selenium抓取视频保存到本地,只爬取了第一页,只要小于等于5分钟的视频。。。
为什么不用requests,没有为什么,就因为有些网站正则和xpath都提取不出来想要的东西,要么就是接口出来的数据加密,要么就因为真正的视频url规律难找!
selenium几行代码轻轻松松就搞定!
安装selenium库,设置无界面模式
代码如下:
fromseleniumimportwebdriver fromselenium.webdriver.chrome.optionsimportOptions importrequests #设置无界面模式 chrome_options=Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') classVideoCrawl(object): video_box=[]#收集video真正的url def__init__(self,url): self.driver=webdriver.Chrome(executable_path=r"C:\ProgramFiles\python\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe",options=chrome_options)#设置无界面模式 self.driver.get(url) #程序运行完毕,析构函数关闭selenium def__del__(self): print("爬取结束。。。。。",len(VideoCrawl.video_box),VideoCrawl.video_box) self.driver.close() defrun(self): self.get_detail_info() #获取列表页所有详情页的url defget_detail_info(self): detail_info=self.driver.find_elements_by_xpath('//a[@class="video-wrapstatpid"]') detail_url=[] foriindetail_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.textforiinvideo_playtime_list] forresinzip(detail_url,video_playtime_list): playtime=res[1].split(":")[0] #print("playtime--------",playtime) ifint(res[1].split(":")[0])<=5:#播放时间小于5分钟的要 #print(res[0],"解析的url",playtime) self.parse_video(res[0],res[1]) else: pass #解析详情页 defparse_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) #类变量统计video_url VideoCrawl.video_box.append(video_url) #保存,请求video_url,二进制保存为mp4 defsave_video(self,url,title,t): filename="video"+title+"-"+t.replace(":","")+".mp4" video=requests.get(url).content withopen(filename,"wb")asfile: file.write(video) print(f"{filename}写入文件完毕") if__name__=='__main__': crawl=VideoCrawl('https://v.huya.com/cat/7') crawl.run()
运行结果如下:
"C:\ProgramFiles\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 Processfinishedwithexitcode-1
都可以正常播放。。。
切记:自己娱乐下练练手删了即可,千万不要用于商业用途哦!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。