|
|
@@ -1,59 +0,0 @@
|
|
|
-import httpx
|
|
|
-import asyncio
|
|
|
-from pathlib import Path
|
|
|
-
|
|
|
-async def download_file(url: str, save_name: str = "book.epub"):
|
|
|
- save_dir = Path.cwd() / ".cache/.browser_use_files"
|
|
|
- save_dir.mkdir(parents=True, exist_ok=True)
|
|
|
-
|
|
|
- # 提取域名作为 Referer,这能骗过 90% 的防盗链校验
|
|
|
- from urllib.parse import urlparse
|
|
|
- parsed_url = urlparse(url)
|
|
|
- base_url = f"{parsed_url.scheme}://{parsed_url.netloc}/"
|
|
|
-
|
|
|
- # 如果没传 save_name,自动从 URL 获取
|
|
|
- if not save_name:
|
|
|
- import unquote
|
|
|
- # 尝试从 URL 路径获取文件名并解码(处理中文)
|
|
|
- save_name = Path(urlparse(url).path).name or f"download_{int(time.time())}"
|
|
|
- save_name = unquote(save_name)
|
|
|
-
|
|
|
- target_path = save_dir / save_name
|
|
|
-
|
|
|
- headers = {
|
|
|
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
|
- "Accept": "*/*",
|
|
|
- "Referer": base_url, # 动态设置 Referer
|
|
|
- "Range": "bytes=0-", # 有时对大文件下载有奇效
|
|
|
- }
|
|
|
-
|
|
|
- try:
|
|
|
- print(f"🚀 开始下载: {url[:60]}...")
|
|
|
-
|
|
|
- # 使用 follow_redirects=True 处理链接中的 redirection
|
|
|
- async with httpx.AsyncClient(headers=headers, follow_redirects=True, timeout=60.0) as client:
|
|
|
- async with client.stream("GET", url) as response:
|
|
|
- if response.status_code != 200:
|
|
|
- print(f"❌ 下载失败,HTTP 状态码: {response.status_code}")
|
|
|
- return
|
|
|
-
|
|
|
- # 获取实际文件名(如果服务器提供了)
|
|
|
- # 这里会优先使用你指定的 save_name
|
|
|
-
|
|
|
- with open(target_path, "wb") as f:
|
|
|
- downloaded_bytes = 0
|
|
|
- async for chunk in response.aiter_bytes():
|
|
|
- f.write(chunk)
|
|
|
- downloaded_bytes += len(chunk)
|
|
|
- if downloaded_bytes % (1024 * 1024) == 0: # 每下载 1MB 打印一次
|
|
|
- print(f"📥 已下载: {downloaded_bytes // (1024 * 1024)} MB")
|
|
|
-
|
|
|
- print(f"✅ 下载完成!文件已存至: {target_path}")
|
|
|
- return str(target_path)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- print(f"💥 发生错误: {str(e)}")
|
|
|
-
|
|
|
-if __name__ == "__main__":
|
|
|
- url = "https://dln1.ncdn.ec/books-files/_collection/userbooks/a8d56556945450076fb0487eaaaf53feca9ac50aca0db9572b483c2a25cdc04a/redirection?filename=%E6%89%8B%E6%9C%BA%E6%8B%8D%E7%BE%8E%E7%85%A7%20%E6%89%8B%E6%9C%BA%E6%91%84%E5%BD%B1%E6%9E%84%E5%9B%BE%2B%E7%94%A8%E5%85%89%2B%E8%89%B2%E5%BD%A9%E6%8A%80%E5%B7%A7%E5%A4%A7%E5%85%A8%20%28%E6%9E%84%E5%9B%BE%E5%90%9B%29%20%28z-library.sk%2C%201lib.sk%2C%20z-lib.sk%29.epub&s=davinci&md5=3DhAjT5f4ntBORB7UEW6SQ&expires=1770906041"
|
|
|
- asyncio.run(download_file(url, "手机拍美照.epub"))
|