Home>Article>Backend Development> Detailed explanation and examples of API calls in python

Detailed explanation and examples of API calls in python

WBOY
WBOY forward
2022-06-16 12:03:19 5039browse

This article brings you relevant knowledge aboutpython, which mainly introduces issues related to API calls, including API calls and data interface calls, request methods, and several Let’s take a look at common API call examples and more. I hope it will be helpful to everyone.

Detailed explanation and examples of API calls in python

Recommended learning:python video tutorial

In daily work, you may need to combine some of the current APIs on the Internet or those provided by the company Data interface to obtain corresponding data or implement corresponding functions.
Therefore, calling APIs and accessing data interfaces are common operations for data analysis. How to quickly implement calls to APIs and data interfaces? Many language versions are generally available online, but the way to trace the source is to use HTTP requests. way to achieve it.

API

API: Simply put, it is a set of protocols, a tool or a set of rules that define communication methods between different applications, hiding the specific implementation process and only exposing it. The parts that must be called are for developers to use.

The above definition is relatively official. Here is a simple example to illustrate. For example, fast food restaurants such as McDonald's outside now use mobile phones to place online orders and pick up meals at the front desk. In this process, consumers usually select the corresponding meal on their mobile phone, click to place an order and pay, and then wait for the front desk to call the number to pick up the meal. We don't know exactly how this process is implemented. The entire process has a corresponding app or small program that communicates to the kitchen data, and then the chef prepares the meal. This APP and applet serve as corresponding API functions.

To give a simple example, a social platform receives comments in various languages every day. As a corresponding analyst, it is a big problem to deal with complex language data. Some people may say that developing a Model to realize the translation integration function. Although this method sounds feasible, it is costly. Secondly, in order to solve one problem, it has to develop a more difficult problem. This is getting further and further away from the original goal. At this time, we can use the relatively mature domestic translation platform API to directly process the existing data. This is relatively cheap, more convenient and can quickly achieve existing goals. There is no doubt about the role of API here.

Data interface

Data interface: Simply put, it is a set of encapsulated data set passwords, which means sending corresponding parameters according to the corresponding rules, and then returning the corresponding related data information. API calls and data interfaces are very similar in daily calls. Relatively speaking, the API has a wider scope and implements more functions, while the data interface serves more as a data acquisition tool.

For example, large e-commerce companies generally use unified SKU to manage products. For example, as a brand owner, this company will sell on different platforms, and on these platforms The mapped product identification ID is different from the company's SKU. Because the company's SKU is not only based on products but also considers various local warehouses and various models of products, and this mapping is relatively complex.
People who deal with data on different platforms generally cannot directly use the company's database to analyze products, because the granularity is too fine and the analysis is complicated and difficult. At this time, the development can be carried out according to the requirements of the corresponding functions. Some systems develop a separate data interface to provide corresponding companies to avoid directly requesting corresponding information such as complex database processes. However, there is a certain delay in the data interface compared to the real-time database.

API calling and data interface calling

API and data interface are discussed through the previous examples. It is relatively simple to understand roughly, and how to implement API calling and data interface calling is here A brief introduction.
To put it simply, API calls and interface calls are similar to an HTTP request. The main thing to call is to encapsulate the request method, request header, URL, and request body according to the corresponding rules and then send the request. This can be achieved Call accordingly.

However, compared with the calls of data interface and API, the general data interface is relatively simple. In many cases, the data interface is used for data access on the company's intranet, so requesting information is relatively simple, while the API is mostly the first The external services developed by third-party enterprises are commercial services. Relatively speaking, in order to ensure the security of the request, more comprehensive considerations are taken, and the addition of AK, SK, signature, timestamp and other information is more complicated.
The two calls to trace back to the source are similar to HTTP requests, and the specific calls are roughly the same. The main reason is that the request parameter information contained in the API call is more. The specific implementation will be briefly introduced below.

The basis of calling - request method

Generally speaking, there are many common HTTP request calling methods. There are many resources in this area. You can check them online. Here we will briefly talk about the common ones. Two request methods.

GET request

The GET request simply obtains resources from the server and can be loaded into the browser's cache.

POST request

POST request generally sends a request to the server in the form of a form. The request parameters included in the request body may lead to the creation and change of resources. Information from POST requests cannot be cached in the browser.
These two request methods are very simple to say, but the most important point is to understand the difference between these two requests, so as to become more familiar with the design of the interface and the use of the API.

The difference between GET and POST requests

1. The maximum request length of GET request is 1024kb, and POST has no limit on request data. The reason for this is that many times GET requests put the corresponding information in the URL, and the length of the URL is limited, resulting in a certain limit on the length of the GET request. The corresponding parameter information of the POST request is placed in the request body, so it is generally not subject to length restrictions.
2. POST request is more secure than GET, because the URL in the GET request contains the corresponding information, the page will be cached by the browser, and other people can see the corresponding information.
3.GET generates one TCP data packet, and POST generates two TCP data packets.
When making a GET request, the header and data are sent together, and then the server responds with 200. POST first sends the header, waits for the server to respond with 100, then sends data, and finally the server responds with 200. But note here that the POST request is divided into two times, but the request body is sent immediately after the header, so this The time in between can be negligible.
4. GET requests only support URL encoding, while POST has a variety of encoding methods.
5.GET request parameters are passed through the URL, multiple parameters are connected with &, and POST requests are placed in the request body.
6.GET requests only support ASCII characters, while POST has no restrictions.
Generally speaking, the GET request can be directly accessed by entering the URL in the browser.

Python implements GET requests and POST requests

The above has introduced some data interfaces, API-related knowledge and request methods in a large amount of space. It is relatively simple to use. The following can be summarized Get familiar with the corresponding request methods. Generally, you can use Python's request library directly.

GET request
import request # GET请求发送的参数一定要是字典的形式,可以发送多个参数。 # 发送格式:{'key1':value1', 'key2':'value2', 'key3', 'value3'} # 样例不能运行 url ='http://www.xxxxx.com' params = {'user':'lixue','password':111112333} requests.get(url,data = parms)
POST request

POST requests generally have three submission forms: application/x-www-form -urlencoded, multipart/form-data, application/json.
Specifically check which of the three request methods are: Google Chrome Check → Network → Select to load the file → Headers → Reuqest Headers → Content-Type
The specific encoding methods are the following three. You can understand the specific request implementation. Generally, the company's internal data interface is set up with a local area network, so some do not need to add a header.

Three submission forms of POST requests

1. The most common post submission data is mainly form form: application/x-www-form-urlencoded

import request data={'k1':'v1','k2':'v2'} headers= {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'} requests.post(url,headers = headers,data=data)

2. Submit data in json format: application/json

data = {'user':'lixue','password':12233} data_json = json.dumps(params) requests.post(url,headers = headers,data = data_json)

3. Generally used to transfer files (rarely used by crawlers): multipart/form-data

files = {'files':open('c://xxx.txt','rb')} requests.post(url = url,headers = headers,files = files)

An example of a simple API request

Through the above brief introduction, you can have a general understanding of specific requests. Here is a simple API aggregation center that provides many useful functions. The following uses this simple API as a simple demonstration API address.
This small example uses the weather API interface to obtain the weather in the past 15 days. Remember to obtain the corresponding apiKey and check the specific usage documentation before using this API. This API website generally provides a certain number of free times for the corresponding API, which can be used for learning, and supports GET and POST requests. Just right for practicing.

GET request
params = { "apiKey":'换成你的apikey', "area":'武汉市', } url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' response = requests.get(url,params) print(response.text)
POST request

The POST request here corresponds to the most common post submission data above, mainly in the form form: application/x-www- form-urlencoded

url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' params = { "apiKey":'换成你的apikey', "area":'武汉市武昌区', } response = requests.post(url,params) print(response.text)

When calling this API interface, you generally need to perform a status code and other return information test to check whether the request is normal. You can give a reference as follows.

params = { "apiKey":'换成你的apikey, "area":'武汉市', } url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' response = requests.post(url,params) print(response.text) if response.status_code != 200: raise ConnectionError(f'{url} status code is {response.status_code}.') response = json.loads(response.content) if 'desc' not in response.keys(): raise ValueError(f'{url} miss key msg.') if response['desc'] != '请求成功': print(11)
Data Extraction

In fact, the API call is very simple, but the core of it is actually the extraction of data from the returned information. Generally speaking, the returned information is in json form and needs to be used from it. Data is extracted using dictionary key-value pairs. The following block returns the corresponding information based on the requested data and extracts it. The obtained information will be shown later.

import requestsimport pandas as pd import numpy as npimport jsondef get_url(area): url = 'https://api.apishop.net/common/weather/get15DaysWeatherByArea' params = { "apiKey":'换成你的apikey', "area":area, } response = requests.get(url,params) if response.status_code != 200: raise ConnectionError(f'{url} status code is {response.status_code}.') response = json.loads(response.content) if 'desc' not in response.keys(): raise ValueError(f'{url} miss key msg.') if response['desc'] != '请求成功': print(11) return responsedef extract_data(web_data): data= web_data['result']['dayList'] weather_data = pd.DataFrame(columns = ['city','daytime','day_weather','day_air_temperature','day_wind_direction','day_wind_power', 'night_weather','night_air_temperature','night_wind_direction','night_wind_power']) for i in range(len(data)): city = data[i]["area"] daytime = data[i]["daytime"] daytime = daytime[:4]+'-'+daytime[4:6]+'-'+daytime[-2:] day_weather = data[i]["day_weather"] day_air_temperature = data[i]['day_air_temperature'] day_wind_direction = data[i]["day_wind_direction"] day_wind_power = data[i]['day_wind_power'] night_weather = data[i]['night_weather'] night_air_temperature = data[i]["night_air_temperature"] night_wind_direction = data[i]['night_wind_direction'] night_wind_power = data[i]["night_wind_power"] c = {"city": city,"daytime": daytime,"day_weather":day_weather,"day_air_temperature":day_air_temperature, "day_wind_direction":day_wind_direction,"day_wind_power":day_wind_power,"night_weather":night_weather, "night_air_temperature":night_air_temperature,"night_wind_direction":night_wind_direction, "night_wind_power":night_wind_power} weather_data = weather_data.append(c,ignore_index = True) weather_data.to_excel(r"C:\Users\zhangfeng\Desktop\最近十五天天气.xlsx",index = None) return weather_dataif __name__ == '__main__': print("请输入对应的城市") web_data = get_url(input()) weather_data = extract_data(web_data)

Part of the results are as follows:
Detailed explanation and examples of API calls in python

Data interface example

In daily learning, the use of data interfaces may be relatively rare. In most cases, the scenario is used to retrieve data within the company, so it is rarely seen. Here we show the use of two data interfaces encountered in work. Due to work considerations, the code shown is a sample. , and cannot be called. You can refer to the calling implementation and specifications.

POST请求调用数据接口
# 销售状态查询def id_status(id_dir): id_data = pd.read_excel(id_dir,sheet_name="Sheet1") id_data.columns = ['shop', 'Campaign Name','Ad Group Name','Item Id'] # 方便后期处理更改列名 id_data["Item Id"] = id_data["Item Id"].astype(str) id_list = list(id_data['Item Id']) print(len(id_list)) id_list = ','.join(id_list) if isinstance(id_list, int): id_list = str(id_list) id1 = id_list.strip().replace(',', ',').replace(' ', '') request_url = "http://xxx.com" # 通过item_id查询id状态 params = { "item_id":id1, } data_json = json.dumps(params) # 属于POST第二种请求方式 response = requests.post(request_url, data = data_json) print(response.text) if response.status_code != 200: raise ConnectionError(f'{request_url} status code is {response.status_code}.') response = json.loads(response.content) if 'message' not in response.keys(): raise ValueError(f'{request_url} miss key msg.') if response['message'] != 'ok': print(11) data= response['result'] ad_data = pd.DataFrame(columns = ['Item Id','saleStatusName']) for j in range(len(data)): item_id =data[j]["item_id"] saleStatusName = data[j]['saleStatusName'] c = {"Item Id": item_id, "saleStatusName": saleStatusName, } ad_data = ad_data.append(c,ignore_index = True) total_data = pd.merge(ad_data,id_data,on ='Item Id', how ='left') df_column = ['shop', 'Campaign Name','Ad Group Name','Item Id','saleStatusName'] total_data = total_data.reindex(columns=df_column) return total_data
GET请求调用数据接口
### 库存数据查询def Smart_investment_treasure(investment_dir): product_data = pd.read_excel(investment_dir,sheet_name="product") if len(product_data)>0: product_data['商品ID']=product_data['商品ID'].astype(str) product_list=list(product_data['商品ID']) product_id = ','.join(product_list) else: product_id='没有数据' return product_id def stock_query(investment_dir): product_data = pd.read_excel(investment_dir,sheet_name="product") if len(product_data)>0: product_data['商品ID']=product_data['商品ID'].astype(str) product_list=list(product_data['商品ID']) product_id = ','.join(product_list) else: product_id='没有数据' if isinstance(product_id, int): product_id = str(id) product_id = product_id.strip().replace(',', ',').replace(' ', '') request_url = "http://xxx.com" # 通过ali_sku查询erpsku params = { "product_id":product_id, } response = requests.get(request_url, params) #属于GET请求 if response.status_code != 200: raise ConnectionError(f'{request_url} status code is {response.status_code}.') response = json.loads(response.content) if 'msg' not in response.keys(): raise ValueError(f'{request_url} miss key msg.') if response['msg'] != 'success': print(11) data= response['data']['data'] # requestProductId = id.split(',') id_state=[] overseas_stock=[] china_stock=[] id_list=[] for j in range(len(data)): inventory_data= data[j]['list'] overseas_inventory=0 ep_sku_list=[] sea_test=0 china_inventory=0 test="paused" id_test="" id_test=data[j]['product_id'] for i in range(len(inventory_data)): if inventory_data[i]["simple_code"] in ["FR","DE","PL","CZ","RU"] and inventory_data[i]["erp_sku"] not in ep_sku_list: overseas_inventory+=inventory_data[i]["ipm_sku_stock"] ep_sku_list.append(inventory_data[i]["erp_sku"]) sea_test=1 elif inventory_data[i]["simple_code"] == 'CN': china_inventory+=int(inventory_data[i]["ipm_sku_stock"]) if overseas_inventory>30: test="open" elif overseas_inventory==0 and china_inventory>100: test="open" id_list.append(id_test) overseas_stock.append(overseas_inventory) china_stock.append(china_inventory) id_state.append(test) c={"id":id_list, "id_state":id_state, "海外仓库存":overseas_stock, "国内大仓":china_stock } ad_data=pd.DataFrame(c) return ad_data

几种常见API调用实例

百度AI相关API

百度API是市面上面比较成熟的API服务,在大二期间由于需要使用一些文本打标签和图像标注工作了解了百度API,避免了重复造轮子,当时百度API的使用比较复杂,参考文档很多不规范,之前也写过类似的百度API调用极其不稳定,但最近查阅了百度API参考文档,发现目前的调用非常简单。
通过安装百度开发的API第三方包,直接利用Python调包传参即可使用非常简单。这里展示一个具体使用,相应安装第三方库官方文档查阅。

''' 第三方包名称:baidu-aip 百度API """ 你的 APPID AK SK """ APP_ID = '你的 App ID' API_KEY = '你的 Api Key' SECRET_KEY = '你的 Secret Key' 参考文档:https://ai.baidu.com/ai-doc/NLP/tk6z52b9z ''' from aip import AipNlp APP_ID = 'xxxxxx' API_KEY = '换成你的apikey' SECRET_KEY = '换成你的SECRET_KEY' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) text = "我还没饭吃" # 调用文本纠错 client.ecnet(text)

Detailed explanation and examples of API calls in python

百度地图API

这个API当时为了设计一个推荐体系引入经纬度换算地址,这样为数据计算带来极大的方便,而且对于一般人来说文本地址相比经纬度信息更加直观,然后结合Python一个第三方包实现两个地址之间经纬度计算得出相对的距离。

# https://lbsyun.baidu.com/ # 计算校验SN(百度API文档说明需要此步骤) import pandas as pd import numpy as np import warnings import requests import urllib import hashlib import json from geopy.distance import geodesic location = input("输入所在的位置\n") # "广州市天河区" ak = "ak1111" # 参照自己的应用 sk = "sk111111" # 参照自己的应用 url = "http://api.map.baidu.com" query = "/geocoding/v3/?address={0}&output=json&ak={1}&callback=showLocation".format(location, ak) encodedStr = urllib.parse.quote(query, safe="/:=&?#+!$,;'@()*[]") sn = hashlib.md5(urllib.parse.quote_plus(encodedStr + sk).encode()).hexdigest() # 使用requests获取返回的json response = requests.get("{0}{1}&sn={2}".format(url, query, sn)) data1=response.text.replace("showLocation&&showLocation(","").replace(")","") data = json.loads(data1) print(data) lat = data["result"]["location"]["lat"] lon = data["result"]["location"]["lng"] print("纬度: ", lat, " 经度: ", lon) distance=geodesic((lat,lon), (39.98028,116.30495)) print("距离{0}这个位置大概{1}".format(location, distance))

Detailed explanation and examples of API calls in python

有道API

在网上查阅了很多API,前面介绍的几种API,他们携带的请求参数信息相对比较简单,调用实现和基础请求没啥区别,这里找了一个相对而言比较多的请求参数的API,相对而言这种API数据付费API,它的安全性以及具体的实现都相对复杂,但是更适合商用。下面可以简单看看。

import requests import time import hashlib import uuid youdao_url = 'https://openapi.youdao.com/api' # 有道api地址 translate_text = "how are you!" input_text = "" # 当文本长度小于等于20时,取文本 if(len(translate_text) 20): input_text = translate_text[:10] + str(len(translate_text)) + translate_text[-10:] uu_id = uuid.uuid1() now_time = int(time.time()) app_id = '1111111' app_key = '11111111111' sign = hashlib.sha256((app_id + input_text + str(uu_id) + str(now_time) + app_key).encode('utf-8')).hexdigest() # sign生成 data = { 'q':translate_text, # 翻译文本 'from':"en", # 源语言 'to':"zh-CHS", # 翻译语言 'appKey':app_id, # 应用id 'salt':uu_id, # 随机生产的uuid码 'sign':sign, # 签名 'signType':"v3", # 签名类型,固定值 'curtime':now_time, # 秒级时间戳 } r = requests.get(youdao_url, params = data).json() # 获取返回的json()内容 print("翻译后的结果:" + r["translation"][0]) # 获取翻译内容

翻译后的结果:你好!
这个API调用中引用了几个真正商用中的一些为了安全性等设置的验证信息,比如uuid、sign、timestamp,这几个在API调用中也是老生常谈的几个概念,是比较全面的。下面简单介绍一下。

uuid

uuid码:UUID是一个128比特的数值,这个数值可以通过一定的算法计算出来。为了提高效率,常用的UUID可缩短至16位。UUID用来识别属性类型,在所有空间和时间上被视为唯一的标识。一般来说,可以保证这个值是真正唯一的任何地方产生的任意一个UUID都不会有相同的值。使用UUID的一个好处是可以为新的服务创建新的标识符。是一种独特的唯一标识符,python 第三方库uuid 提供对应的uuid生成方式,有以下的几种 uuid1(),uuid3(),uuid4(),uuid5()上面采用的是uuid1()生成,还可以使用uuid4()生成。

sign

sign:一般为了防止被恶意抓包,通过数字签名等保证API接口的安全性。为了防止发送的信息被串改,发送方通过将一些字段要素按一定的规则排序后,在转化成密钥,通过加密机制发送,当接收方接受到请求后需要验证该信息是否被篡改过,也需要将对应的字段按照同样的规则生成验签sign,然后在于后台接收到的进行比对,可以发现信息是否被串改过。在上面的例子利用hashlib.sha256()来进行随机产生一段密钥,最后使用.hexdigest()返回最终的密钥。
curtime:引入一个时间戳参数,保证接口仅在一分钟内有效,需要和客户端时间保持一致。避免重复访问。

推荐学习:python视频教程

The above is the detailed content of Detailed explanation and examples of API calls in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete