隨著網絡時代的到來,各種網絡媒體如雨后春筍般地涌現,各類視頻平臺也層出不窮,而騰訊視頻便是其中頗為知名的一家。本文將介紹如何使用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解析,我們可以獲取到各視頻的詳細信息。希望對您有所幫助!