Use API to retrieve data from internet
Reference: Working with APIs
Many big companies and organizations provide API for us to retrieve data, and we use requests library to get data with Json format from them.
1: Type of requests
There are many types of request for getting data, We can use a simple GET request to retrieve information from the OpenNotify API. OpenNotify has several API endpoints. An endpoint is a server route that is used to retrieve different data from the API. For example, the /comments
endpoint on the Reddit API might retrieve information about comments, whereas the/users
endpoint might retrieve data about users.
For example:
# Make a get request to get the latest position of the international space station from the opennotify api.
response = requests.get("http://api.open-notify.org/iss-now.json")
status_code = response.status_code
And you can see a listing of all the endpoints on OpenNotify here.
2: Status codes
The request we just made had a status code of 200
. Status codes are returned with every request that is made to a web server. Status codes indicate information about what happened with a request. Here are some codes that are relevant toGET requests:
200
-- everything went okay, and the result has been returned (if any)301
-- the server is redirecting you to a different endpoint. This can happen when a company switches domain names, or an endpoint name is changed.401
-- the server thinks you're not authenticated. This happens when you don't send the right credentials to access an API (we'll talk about this in a later mission).400
-- the server thinks you made a bad request. This can happen when you don't send along the right data, among other things.403
-- the resource you're trying to access is forbidden -- you don't have the right permissions to see it.404
-- the resource you tried to access wasn't found on the server.
# Make a get request to get the latest position of the international space station from the opennotify api.
response = requests.get("http://api.open-notify.org/iss-pass.json")
status_code = response.status_code
3: Query parameters
Look at documentation for the OpenNotify API, we see that the ISS Pass endpoint requires two parameters.
The ISS Pass endpoint returns when the ISS will next pass over a given location on earth. In order to compute this, we need to pass the coordinates of the location to the API. We do this by passing two parameters -- latitude and longitude.
We can make a dictionary with these parameters, and then pass them into the function.
We can also do the same thing directly by adding the query parameters to the url, like this: http://api.open-notify.org/iss-pass.json?lat=40.71&lon=-74
.
For example we send San the coordinates of Francisco as parameters to the API:
response = requests.get("http://api.open-notify.org/iss-pass.json", params={"lat":37.78, "lon":-122.41})
content = response.content
And the content is:
b'{\n "message": "success", \n "request": {\n "altitude": 100, \n "datetime": 1441417753, \n "latitude": 37.78, \n "longitude": -122.41, \n "passes": 5\n }, \n "response": [\n {\n "duration": 369, \n "risetime": 1441456672\n }, \n {\n "duration": 626, \n "risetime": 1441462284\n }, \n {\n "duration": 581, \n "risetime": 1441468104\n }, \n {\n "duration": 482, \n "risetime": 1441474000\n }, \n {\n "duration": 509, \n "risetime": 1441479853\n }\n ]\n}'
The json library has two main methods:
dumps
-- Takes in a Python object, and converts it to a string.loads
-- Takes a json string, and converts it to a Python object.
To see more details about the parameters format JSON, please click the link: JavaScript Object Notation
4: Getting JSON from a request
We can get the content of a response as a Python object by using the .json()
method on the response.
Get the duration value of the first pass of the ISS over San Francisco, and assign the value the first_pass_duration.
# Make the same request we did 2 screens ago.
parameters = {"lat": 37.78, "lon": -122.41}
response = requests.get("http://api.open-notify.org/iss-pass.json", params=parameters) # Get the response data as a python object. Verify that it's a dictionary.
data = response.json()
print(type(data))
print(data)
firt_pass_duration = data["reponse"][0]["duration"]
As we can see just now, we can transform the json format data from the response to dictionary for processing.
5: Content type
The server doesn't just send a status code and the data when it generates a response. It also sends metadata containing information on how the data was generated and how to decode it. This is stored in the response headers. We can access this with the .headers
property of a response.
The headers will be shown as a dictionary. Within the headers, content-type
is the most important key for now. It tells us the format of the response, and how to decode it. For the OpenNotify API, the format is json, which is why we could decode it with json earlier.
Exercise:
OpenNotify has a API endpoint, astros.json. It tells we how many people are currently in space.
We can see the details here.
Here is the sample code:
import requests
parameters = {"lat":39.92889, "lon":116.38833}
response = requests.get("http://api.open-notify.org/astros.json", params=parameters)
data = response.json()
#print(data)
in_space_count = data['number']
print(in_space_count)
Use API to retrieve data from internet的更多相关文章
- Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)
使用Microsoft SQL SERVER 2014 Management Studio访问Azure SQL Database时,查看存储过程时遇到下面错误信息: TITLE: Microsoft ...
- Dynamics CRM2016 Web API之Retrieve Multiple
之前的博文只介绍了通过记录的primary key来查询单条记录或者单个属性值,本篇介绍多条记录的查询方法 var filter = "?$filter=name eq '123'" ...
- C# Web API Modify Post Data Size Limit
在Web.config中增加下面两个配置后,重启IIS即可. 1.修改http请求数据大小限制 <system.web> <httpRuntime maxRequestLength ...
- [原]Android开发环境搭建
[Date]2014-04-20 [Author]wintys (wintys@gmail.com) http://wintys.cnblogs.com [Keywords]android . 离线a ...
- Gluon Data API
http://mxnet.apache.org/api/python/gluon/data.html import sys import os import time import mxnet as ...
- 【转】简单的 Laravel 5 REST API
Introduction Almost all successful internet based companies have APIs. API is an acronym for Applica ...
- puppeteer(五)chrome启动参数列表API
List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...
- Indexing Sensor Data
In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set o ...
- Advanced Architecture for ASP.NET Core Web API
转自: https://www.infoq.com/articles/advanced-architecture-aspnet-core ASP.NET Core's new architecture ...
随机推荐
- left join 和 left outer join 的区别
left join 和 left outer join 的区别 通俗的讲: A left join B 的连接的记录数与A表的记录数同 A right join ...
- 解决windows下Eclipse连接远程Hadoop报错
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.N ...
- XenDesktop 5 PowerShell SDK Primer – Part 2 – Creating Hypervisor Connections and Hosts
One of the new changes that you will see in XenDesktop 5 is the configuration of hypervisor connecti ...
- 浅谈print2flash的在线预览转换应用(原创)
print2flash是一种在线预览转换工具,可以将doc.docx.xls.pdf.ppt等格式的文档转换成flash文件进行预览,因为之前使用的flash2paper只支持32为操作系统,不支持6 ...
- applicationContext.xml 配置文件的存放位置
eb.xml中classpath:和classpath*: 有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中 ...
- 加密传输SSL协议5_Hash Function
怎么对一个大的文件进行签名,因为文件比较大,非对称签名很慢.那么想,我能把这个大的文件通过一种函数变换,变成一个和源文件唯一对应的的小的文件吗?答案是可以的. Hash Function 这里任何的文 ...
- 出现"无法连接synaptics定点装置驱动程序"
"开始“--”运行“--regedit(打开注册表)--依次打开 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVERSION/R ...
- Javascript 字符串浏览器兼容问题
先看下不兼容的写法,若我想获取某个字符串的第几位 var str='aavvvcc'; console.info(str[0]); 这种写法 在IE 7以下的浏览器都不兼容,以下提供浏览器全兼容的方式 ...
- avalon.js 多级下拉框实现
学习avalon.js的时候,有一个多级下拉框的例子,地址 戳这里 代码实现了联动, 但是逻辑上面理解有点难度,获取选择的值 和 页面初始化 功能存在问题. 在写地图编辑的时候,也用到了多级下拉框,特 ...
- php页面相互调用的知识点
目前我们有这样一个需求: (1) a.php 页面要使用 b.php 定义的函数,我们可以使用 如下指令 require require_once include include_once 举 ...