使用 selenium 下載小視頻

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from selenium import webdriver
import urllib
import urllib.request
import time
# 普通爬蟲,使用Charles抓包獲取網址url,但是西瓜視頻url只能使用一次
# url加密,只能使用selenium獲取數據,瀏覽器
url = 'https://www.ixigua.com/'

driver = webdriver.Chrome()


driver.get(url=url)
time.sleep(2)

video_lists = driver.find_elements_by_xpath('//div[@class="feed-infinite-wrapper"]/ul/li')

print(len(video_lists))

hrefs = []
for video_info in video_lists:
    hrefs.append(video_info.find_element_by_xpath('.//a').get_attribute('href'))


def download_video(title, video_url):

    urllib.request.urlretrieve(video_url,'./videos/%s.mp4'%(title))
    print('視頻:%s下載成功'%(title))

for href in hrefs:

    driver.get(href)
    time.sleep(2)

    video_url = driver.find_element_by_class_name('vjs-tech').get_attribute('src')

    title = driver.find_element_by_xpath('//h2[@class="title"]').text
    download_video(title,video_url)
input('輸入任意字符,點擊回車退出程序')

driver.quit()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章