john4real 2022-02-11 20:18 采纳率: 50%
浏览 43
已结题

python用requests get(url)调用github的api提示 HTTPSConnectionPool

问题遇到的现象和发生背景

在练习《python编程 从入门到实践》一书的第17章时,我在调用GitHub的api时总是提示443错误,无论是挂代理还是使用devside car都不能解决问题

问题相关代码,请勿粘贴截图
import requests

url = 'https://api.githun.com/search/reppositories?q=language:python&sort=stars'
r = requests.get(url)

print('Status Code:', r.status_code)

response_dict = r.json()
print(response_dict.keys())
运行结果及报错内容
发生异常: ConnectionError
HTTPSConnectionPool(host='api.githun.com', port=443): Max retries exceeded with url: /search/reppositories?q=language:python&sort=stars (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001F03E0C52B0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

During handling of the above exception, another exception occurred:


During handling of the above exception, another exception occurred:


During handling of the above exception, another exception occurred:

  File "E:\BaiduNetdiskWorkspace\Python代码集合\书籍\Chapter_17\python_repos.py", line 5, in <module>
    r = requests.get(url)


  • 写回答

3条回答 默认 最新

  • CSDN专家-HGJ 2022-02-11 20:49
    关注

    由于其api中需要传入参数不一致,需要headers,有的需要token,对于topics可以用如下方式获取:

    import requests
    
    url = 'https://api.github.com/search/topics?q=python&sort=stars'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43', 'content-type': 'application/json; charset=utf-8'}
    r = requests.get(url,headers=headers)
    
    print('Status Code:', r.status_code)
    
    response_dict = r.json()
    print(response_dict)
    

    如有帮助,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月20日
  • 已采纳回答 2月12日
  • 创建了问题 2月11日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵