温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中selenium如何抓取虎牙短视频

发布时间:2021-07-02 11:22:19 来源:亿速云 阅读:174 作者:小新 栏目:开发技术

小编给大家分享一下Python中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)     #程序运行完毕,析构函数关闭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('//h2[@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   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

Python中selenium如何抓取虎牙短视频

都可以正常播放。。。

切记:自己娱乐下练练手删了即可,千万不要用于商业用途哦!

以上是“Python中selenium如何抓取虎牙短视频”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI