代码说明
开发工具: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}")