test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import httpx
  2. import asyncio
  3. from pathlib import Path
  4. async def download_file(url: str, save_name: str = "book.epub"):
  5. save_dir = Path.cwd() / ".cache/.browser_use_files"
  6. save_dir.mkdir(parents=True, exist_ok=True)
  7. # 提取域名作为 Referer,这能骗过 90% 的防盗链校验
  8. from urllib.parse import urlparse
  9. parsed_url = urlparse(url)
  10. base_url = f"{parsed_url.scheme}://{parsed_url.netloc}/"
  11. # 如果没传 save_name,自动从 URL 获取
  12. if not save_name:
  13. import unquote
  14. # 尝试从 URL 路径获取文件名并解码(处理中文)
  15. save_name = Path(urlparse(url).path).name or f"download_{int(time.time())}"
  16. save_name = unquote(save_name)
  17. target_path = save_dir / save_name
  18. headers = {
  19. "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",
  20. "Accept": "*/*",
  21. "Referer": base_url, # 动态设置 Referer
  22. "Range": "bytes=0-", # 有时对大文件下载有奇效
  23. }
  24. try:
  25. print(f"🚀 开始下载: {url[:60]}...")
  26. # 使用 follow_redirects=True 处理链接中的 redirection
  27. async with httpx.AsyncClient(headers=headers, follow_redirects=True, timeout=60.0) as client:
  28. async with client.stream("GET", url) as response:
  29. if response.status_code != 200:
  30. print(f"❌ 下载失败,HTTP 状态码: {response.status_code}")
  31. return
  32. # 获取实际文件名(如果服务器提供了)
  33. # 这里会优先使用你指定的 save_name
  34. with open(target_path, "wb") as f:
  35. downloaded_bytes = 0
  36. async for chunk in response.aiter_bytes():
  37. f.write(chunk)
  38. downloaded_bytes += len(chunk)
  39. if downloaded_bytes % (1024 * 1024) == 0: # 每下载 1MB 打印一次
  40. print(f"📥 已下载: {downloaded_bytes // (1024 * 1024)} MB")
  41. print(f"✅ 下载完成!文件已存至: {target_path}")
  42. return str(target_path)
  43. except Exception as e:
  44. print(f"💥 发生错误: {str(e)}")
  45. if __name__ == "__main__":
  46. 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"
  47. asyncio.run(download_file(url, "手机拍美照.epub"))