Selenium + 视频网站 使用方法详解

代码说明

开发工具:pycharm
开发语言:python
实施工具:chrome
代码功能:
运行后,能够根据关键字 打开谷歌浏览器,自行搜索关键词,并进行down视频

完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import youtube_dl
import os
import multiprocessing

def dwl_vid(vidoelink=[]):
    ydl_opts = {}  # 用以规定视频参数
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([vidoelink])
        time.sleep(50)
    print("下载完成")






def getvideos(key,count_videos,sleeptime=5):
    '''
    :param key: 搜索关键字
    :param count: 下载视频个数
    :param sleeptime: 每次加载视频列表等待时间,和网速有关
    :return:
    '''
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument(" disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'D:\chromedriver.exe')
    driver.get("https://www.有图币.com/")
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys(key)
    driver.find_element(By.CSS_SELECTOR, "button.style-scope.ytd-searchbox#search-icon-legacy").click()
    count = 10000 * 100
    videos = []
    video_last=[]
    error=0
    while len(set(videos)) < count_videos or error>5:
        video_last=videos
        js = f"var q=document.documentElement.scrollTop={count}"  # 滚动到最下面
        # js="var q=document.documentElement.scrollTop=0"  # 滚动到最上面
        driver.execute_script(js)
        time.sleep(sleeptime)
        times=3
        while times>0:
            try:
                listvideo = [my_href.get_attribute("href") for my_href in WebDriverWait(driver, 10).until(
                    EC.visibility_of_all_elements_located(
                        (By.CSS_SELECTOR, "a.yt-simple-endpoint.style-scope.ytd-video-renderer#video-title")))]
                times=-1
            except:
                print(f"将重新尝试{times}次")
                listvideo=[]
                times=times-1
        if listvideo==0:
            error=error+1
        print(len(listvideo), listvideo)
        videos = videos.__add__(listvideo)
        new_video=[i for i in videos if i not  in video_last]
        print(f"新收集到以下视频{new_video}")
    print(len(videos), set(videos))
    # print(len(set(videos)))
    end_videos=[]
    for i in set(videos):
        end_videos.append(i)
    return end_videos
def downvideos(listvideo,outpath):
    print("进入下载函数",listvideo)
    for i in listvideo:
        dwl_vid(i)
    #移动视频
    if not os.path.exists(outpath):
        os.mkdir(outpath)
    files=os.listdir("./")
    for i in files:
        if i.endswith(".mp4") or i.endswith(".avi") :
            os.rename(i,outpath+"/"+str(len(os.listdir(outpath))).zfill(5)+".mp4")
    print("已完成")
    return ""
if __name__ == '__main__':
    key="play phone"
    videolsit=[]
    count_videos=10
    while len(videolsit)<5:
        videolsit=videolsit+getvideos(key=key,count_videos=count_videos,sleeptime=5)

    downvideos(videolsit,f"./{key}")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值