Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6985
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2022-12-20 17:00
文章分类
文章存档

2024年(1)

2023年(20)

我的朋友
最近访客

分类: Python/Ruby

2024-07-31 11:47:56

在使用Python编程进行数据集下载时,遇到程序突然中断的情况并不罕见。这种情况通常由多种因素引起,如网络不稳定、API请求数量限制或SSL握手失败,尤其是SSL握手失败导致下载停滞都是属于我们工作中常见的了。此类问题,我们可以采取一些措施来解决,以此来推进我们的项目进度。

一、网络不稳定的问题


首先,我们来说说网络不稳定的问题。网络不稳定通常表现为连接中断、速度缓慢或无法建立连接,我们能很直观的看出来。这种情况下,可能是由于我们的网络环境复杂、互联网流量的变化、或者隔墙防火墙等问题导致。

处理网络不稳定的问题,我们可以试试:

  1. 重试机制:通过编程实现自动重试,可以有效应对短暂的网络不稳定。

点击(此处)折叠或打开

  1. import time
  2. import requests
  3. from requests.adapters import HTTPAdapter
  4. from requests.packages.urllib3.util.retry import Retry
  5. def download_with_retry(url, path, retries=5):
  6.     session = requests.Session()
  7.     retry = Retry(total=retries, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
  8.     adapter = HTTPAdapter(max_retries=retry)
  9.     session.mount('http://', adapter)
  10.     session.mount('https://', adapter)
  11.     
  12.     try:
  13.         response = session.get(url, stream=True)
  14.         with open(path, 'wb') as f:
  15.             for chunk in response.iter_content(1024):
  16.                 if chunk:
  17.                     f.write(chunk)
  18.     except requests.exceptions.RequestException as e:
  19.         print(f"Failed to download file: {e}")



  1. 使用短效代理IP:引入优质的短效代理IP,可以提高网络请求的成功率和稳定性,这边的话,我目前在用的是青果网络,成功率够高,稳定性也不错。我知道这时候有人会跳出来说快代理、芝麻代理之类的同类型代理IP服务提供商了,不过我个人使用下来,青果网络的综合性价比会更高一点。当然这个会根据每个人的项目不一样,以及其他条件不同,得出不一样的结论。目前几乎市面上所有的代理IP服务提供商都有提供测试,大家可以自行了解测试,根据自己需要再做决定。

二、API 请求数量限制的问题


API请求数量限制通常由服务提供商(如Kaggle)设定,以防止过度使用其服务。这种限制会导致当请求过多时,会碰到“手滑”被暂时屏蔽的问题。

针对API请求数量限制的这个问题,我们可以尝试:

  1. 请求速率控制:通过编程方式控制请求速率,避免在短时间内发送过多请求。

点击(此处)折叠或打开

  1. import time
  2. def request_with_rate_limiting(url, interval=2):
  3.     response = requests.get(url)
  4.     time.sleep(interval)  # Wait for the interval period
  5.     return response



  1. 轮换代理IP:使用多个代理IP进行请求轮换,可以有效分散单一IP的请求负载,避免触发数量限制。

以下是部分代码,大家可以参考一下:


点击(此处)折叠或打开

  1. import os
  2. import time
  3. import requests
  4. from requests.adapters import HTTPAdapter
  5. from requests.packages.urllib3.util.retry import Retry
  6. PROXY_POOL = [
  7.     "",
  8.     # 青果网络的代理IP,更多代理IP请登录青果网络获取
  9. ]
  10. def get_random_proxy():
  11.     return {"http": PROXY_POOL[time.time() % len(PROXY_POOL)], "https": PROXY_POOL[time.time() % len(PROXY_POOL)]}
  12. def download_file_with_proxy(url, path, proxies, retries=5):
  13.     session = requests.Session()
  14.     retry = Retry(total=retries, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
  15.     adapter = HTTPAdapter(max_retries=retry)
  16.     session.mount('http://', adapter)
  17.     session.mount('https://', adapter)
  18.     headers = {}
  19.     if os.path.exists(path):
  20.         existing_file_size = os.path.getsize(path)
  21.         headers['Range'] = f'bytes={existing_file_size}-'
  22.     else:
  23.         existing_file_size = 0
  24.     
  25.     response = session.get(url, headers=headers, proxies=get_random_proxy(), stream=True)
  26.     total_size = int(response.headers.get('content-length', 0)) + existing_file_size
  27.         
  28.     with open(path, 'ab') as file:  # Open in append mode
  29.         for chunk in response.iter_content(chunk_size=1024):
  30.             if chunk:
  31.                 file.write(chunk)
  32. # 使用青果网络代理IP进行下载
  33. download_file_with_proxy('', 'largefile.zip', PROXY_POOL)
希望这些方法能在你的项目中有所助益,帮助你解决我们在使用Python编程进行数据集下载时,遇到程序突然中断的情况。如果还有后续的疑问,也可以在评论区给我留言,如果我看到了会及时回复。
阅读(64) | 评论(0) | 转发(0) |
0

上一篇:dis ip int brief命令的作用是什么?

下一篇:没有了

给主人留下些什么吧!~~