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

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 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题