python 爬腾讯视频
随着网络时期的到来,各种网络媒体如雨后春笋般地出现,各类视频平台也层见叠出,而腾讯视频便是其中很是知名的一家。本文将介绍怎样使用Python爬取腾讯视频的信息。
#导入必要的库 import requests from lxml import etree headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } # 获得所有视频 def get_all_videos(): url = 'https://v.qq.com/' response = requests.get(url, headers=headers) html = etree.HTML(response.text) # 获得所有视频的 url videos_urls = html.xpath('//div[@class="mod_title"]/a/@href') # 获得所有视频的标题 videos_title = html.xpath('//div[@class="mod_title"]/a/text()') for url, title in zip(videos_urls, videos_title): print(url, title) # 获得指定视频的信息 def get_video_info(video_url): response = requests.get(video_url, headers=headers) html = etree.HTML(response.text) # 获得视频名称 video_title = html.xpath('//h1[@class="player_title"]/text()')[0] # 获得视频时长 video_duration = html.xpath('//span[@class="duration"]/text()')[0] # 获得视频发布时间 video_pubtime = html.xpath('//span[@class="pub"]/text()')[0] print(video_title, video_duration, video_pubtime) if __name__ == '__main__': get_all_videos() get_video_info('https://v.qq.com/x/cover/2xrh1eoyttpb034.html')
以上便是使用Python爬取腾讯视频的方法,通过网络要求和XPath解析,我们可以获得到各视频的详细信息。希望对您有所帮助!
文章来源:丸子建站
文章标题:python 爬腾讯视频
https://www.wanzijz.com/view/73900.html