You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用requests.get请求Github链接失败,其他网站可正常访问如何解决

报错核心原因

该报错属于DNS域名解析失败,系统无法将raw.githubusercontent.com域名转换为对应的服务器IP地址,其他网站可正常访问说明基础网络连通性无问题,仅Github相关域名的解析链路异常。

解决方案

  • 方案1:手动修改hosts文件绑定域名解析
    • Windows系统:用管理员权限打开C:\Windows\System32\drivers\etc\hosts文件,在末尾添加如下解析记录,保存后执行cmd命令ipconfig /flushdns刷新DNS缓存即可
      185.199.108.133 raw.githubusercontent.com
      140.82.113.4 github.com
      
    • Linux/macOS系统:用sudo权限编辑/etc/hosts文件,添加上述相同的解析记录,保存后执行sudo dscacheutil -flushcache(macOS)或sudo systemctl restart nscd(Linux)刷新缓存
  • 方案2:调整请求的重试和超时配置
    若使用requests库发起请求,可手动增加重试次数、延长超时时间,示例代码如下:
    import requests
    from requests.adapters import HTTPAdapter
    from urllib3.util.retry import Retry
    
    session = requests.Session()
    # 配置重试策略
    retry_strategy = Retry(
        total=5,
        backoff_factor=1,
        status_forcelist=[429, 500, 502, 503, 504]
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    session.mount("https://", adapter)
    session.mount("http://", adapter)
    
    url = "https://raw.githubusercontent.com/jana-hoh/gdp/main/DP_LIVE_22102021141534889.csv"
    try:
        resp = session.get(url, timeout=30)
        resp.raise_for_status()
        # 后续可直接处理响应内容,比如用pandas读csv可写pd.read_csv(resp.content)
    except Exception as e:
        print(f"请求出错: {e}")
    
  • 方案3:配置网络代理
    若处于国内网络环境,可在请求参数中添加合规的代理配置:
    proxies = {
        "http": "http://你的代理地址:端口",
        "https": "http://你的代理地址:端口"
    }
    resp = requests.get(url, proxies=proxies, timeout=30)
    

内容的提问来源于stack exchange,提问作者Jana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 11:06:07