Python实战项目(一)使用API

来源:互联网 发布:淘宝客服旺旺如何设置 编辑:程序博客网 时间:2024/05/18 00:21

1、首先要知道API调用请求数据:
在浏览器的地址栏中输入:https://api.github.com/search/repositories?q=language:python&sort=start
这个调用就返回了Github当前托管了多少个python项目。
2、准备工作:
由于我的电脑是Windows系统,作为一个技术小白用了大半个上午安装好了pip,并利用pip安装requests包。

(1)在Windows系统中检查是否安装了pip:在终端窗口执行如下命令:

  python -m pip --version  

在Windows下安装pip:可以百度到网址
运行命令:python get-pip.py
(2)安装requests的命令:

pip install requests

3、处理API响应:python_repos.py

import requests#执行API调用并储存响应url='https://api.github.com/search/repositories?q=language:python&sort=stars'r=requests.get(url)print()#将API响应储存在一个变量中response_dict =r.json()#处理结果print(response_dict.keys())