[Asm] 纯文本查看 复制代码 from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests,time,tqdm
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36',
}
def get_url():
# 实例化浏览器对象
driver = webdriver.Chrome(executable_path=r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
for i in range(3):
try:
driver.get(url)
wait = WebDriverWait(driver,10)
wait.until(EC.presence_of_element_located((By.ID,"PLAYERS")))
iframe = driver.find_element_by_id("PLAYERS")
#进入嵌套网页
driver.switch_to.frame(iframe)
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "dplayer-video")))
mov_url = driver.find_element_by_class_name("dplayer-video").get_attribute('src')
if mov_url!="":
driver.quit() #退出浏览器
return mov_url
else:
driver.quit() #退出浏览器
time.sleep(2)
print("正在重新请求")
continue
except:
print("发生错误,正在重新请求")
driver.quit() #退出浏览器
continue
#请求函数
def html_response(url):
for i in range(3):
try:
resp = requests.get(url,headers=headers,stream=True,timeout=20) # stream(可选)如果为“False”,响应内容将被立即下载
if resp.status_code ==200:
return resp
except Exception:
print(Exception)
time.sleep(2)
print("正在重新请求")
continue
#添加进度条和下载
def mov_down(url):
resp = html_response(url)
#视频长度
file_size = int(resp.headers.get('Content-Length'))
print(file_size)
#初始化进度条长度
pbar = tqdm.tqdm(total=file_size)
with open('电影下载.mp4','wb') as f:
for chunk in resp.iter_content(1024*1024*2):
f.write(chunk)
pbar.set_description("正在下载中") #设置修改进度条的描述。参数
pbar.update(1024*1024*2) #手动更新进度条,对读取文件等流很有用
if __name__ == '__main__':
jx_url=input("请输入需要解析的网址:")
if jx_url!="":
url = f"https://jx.aidouer.net/?url={jx_url}"
mov_url = get_url()
print(mov_url)
mov_down(mov_url)
print("下载完成")
|