current position:Home>Python implements API calls

Python implements API calls

2022-09-09 03:35:08web15286201346

在日常工作中,Some may need to be combined with online nowAPIOr data interface provided by the company to get the corresponding data or implement the corresponding function.
因此APIThe invocation of the data analysis and data access interface is do a common operations,如何快速实现APIAnd data interface call,Online generally provide many languages,But their roots's approach are usedHTTPRequest to implement.Because I usedPython,This article mainly adopts alsoPythonAs a tool to introduce how to operate.

文章目录

API

API:Is simply a set of agreement、A tool or a set of rules,Define the communication method between different applications,The concrete implementation process hidden,Only part of calls must be exposed to developers to use.

The above definition is the official,Simple example to explain here,Such as mobile phone outside McDonald's fast food restaurants are taken now online order,The process of front desk take dinner.而在这个过程,As consumers in the mobile phone usually choose good corresponding products and then click on the order of payment,And then wait for the front desk station take meal can be.And the process how to realize we don't know,The process has the correspondingappOr small program data communications to the kitchen,And then cook a meal.而这个APPAnd small program will act as the correspondingAPI功能.

举一个简单的例子,A social platform it comments received every day all kinds of language information,And as the analysis of the corresponding personnel in the face of complex language data processing,是一个大难题,Someone might say to develop a model to implement the integration of translation function,This method is it sounds feasible but cost price high,Secondly in order to solve a problem,And to develop a more difficult problem.This deviates from target more and more far,This time you can use the mature translation platform in ChinaAPI,Deal directly with the existing data.This relatively small cost price,More convenient and more quickly achieve the goal of the existing.而APIThe role of here is no doubt.

数据接口

数据接口: Is simply a set of encapsulation data set password,Is in accordance with the rules of the corresponding send corresponding parameter,And then returns the corresponding data information.APICalls and data interface of the two is very similar in daily call,相对而言APIThe scope of the broader,实现的功能也比较多,And daily ACTS as the data interface is a tool to take a few more.

Such as large electricity company generally use unifiedSKUTo manage the goods,And such as the company as a brand dealer,It will be on different platforms to sell,And on these platforms mapping logosIDDifferent from the companySKU.因为公司的SKUBased not only on goods but also to consider the local warehouse as well as the product of each model,And this map is relatively more complex.
In dealing with different platforms typically have no direct data using the company's database to analyze the goods,Because the granularity is too thin,Analysis of more complex difficult,This time you can according to the requirement of the corresponding function to develop in the existing system is to develop a separate data interface to provide the corresponding company,Avoid direct request database process corresponding information such as the complex.But relatively real-time database data interface has certain delay.

APICall and data interface call

APIAnd data interface through the previous example is discussed,Roughly and more easy to understand,And specific how to realizeAPIThe call and data interface call here a brief introduction of.
简单来说APIThe call and the call of the interface are similar to aHTTP请求,And call the most main is according to the corresponding rules will ask way、请求头、URL、As well as the request body good encapsulation and then send the request,You can achieve the corresponding call.

But the data interface andAPITwo calls in comparison to,General data interface is simple,In many cases, the data interface is on the company Intranet data access so request information is simple,而APIMostly in the form of third-party enterprise development foreign service belongs to a kind of commercial service,Relative to character in order to ensure the safety of the request,Consider a more comprehensive,加入了AK、SK、签名、The information such as timestamp is complicated.
And check this two calls are similarHTTP请求,Specific call approximately the,主要就是APICall information contained in the request parameters more.And how to implement the following specific will simply introduce.

The basis of the call-请求方法

一般而言,常见的HTTPRequest calling way has a lot of,This aspect of resources more,Can online access to,Here is a simple about two common request method.

GET 请求

GETThe request is simply from access to resources on the server,Can be loaded into the browser's cache.

POST 请求

POSTRequest generally in the form of form sends a request to the server,Request parameters included in the request body may result in resources to create and change.POSTThe requested information cannot be cached in the browser.
These two request method is very simple,But the most important thing is to know the difference between these two requests,For the design of the interface andAPI的使用更加熟悉.

GET和POST请求的区别

1.GETRequest length up to1024kb,POST对请求数据没有限制.This reason is that most of the timeGETRequest to the corresponding information inURL中,而URL的长度有限,导致GETThe length of the request also is limited by a certain.而POSTRequest the corresponding parameter information in the request bodybodySo in general without being limited by the length of.
2.POST请求比GET更安全一些,因为GET请求中URLContains the corresponding information,页面会被浏览器缓存,Others can see the corresponding information.
3.GET产生一个TCP数据包,POST产生两个TCP数据包.
GET请求的时候将header、data一起发送出去,Then the server response back200.而POST则是先发送header,等待服务器响应100,然后发送data,The server response back200.But note here,POSTThe request is divided into two,But request bodybody是紧随在header之后发送的,So this time between can almost no.
4.GET请求只支持URL编码,而POSTRelatively have a variety of encoding.
5.GET请求参数是通过URL传递的,多个参数以&连接,POST请求放在request body中.
6.GET请求只支持ASCII字符,而POST没有限制.
In general the browser input url can direct access to the general isGET请求.

Python实现GET请求和POST请求

Introduces some data interface much above、APIRelated knowledge and request method,使用起来比较简单,The following can be a general familiarity with the way the corresponding request.一般直接使用Python的request库就可以.

GET请求
import request
# GETRequest parameter must be a dictionary in the form of,可以发送多个参数.
# 发送格式:{'key1':value1', 'key2':'value2', 'key3', 'value3'}
# The sample can not run
url ='http://www.xxxxx.com'
params = {'user':'lixue','password':111112333}
requests.get(url,data = parms)
POST请求

POSTThere are three kinds of requests in general submission form:application/x-www-form-urlencoded、multipart/form-data、application/json.
Specific view is three way of what kind of request:Google browser check → Network →选择加载文件 → Headers → Reuqest Headers → Content-Type
Specific encoding for the following three,Can understand the specific request to realize,General internal data interface can do not need to set up a local area network (LAN) so haveheader.

POSTRequest of three kinds of form

1.最常见的post提交数据以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.以json格式提交数据:application/json

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

3.一般用来传文件(爬虫很少用到):multipart/form-data

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

一个简单API请求的实例

通过上面简单的介绍,For a specific request an idea,Here collect a simpleAPIThe aggregation center,Provides a lot of good with the function of the.Here in this simpleAPIAs a simple demonstrationAPI地址.
This small example is the weatherAPIInterface to get close to15天的天气.在使用这个APIRemember to get the correspondingapiKeyAnd view the use of specific document.这个APISite generally to the appropriateAPIProvide a certain amount of free times,Can serve as learning to use,而且支持GET和POST请求.Just for try.

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

这里的POSTThis request is also one of the most commonpost提交数据以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)

在调用这种APIInterface is generally need to return a status code, as well as information test,检查请求是否正常,Can according to the following reference given.

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)
数据提取

其实API的调用很简单,But one of the most core is return information in data extraction,In general the information returned isjson形式,Need to use a dictionary keys to extract data,Below the returned to the corresponding information according to the request of data and extract,Behind the access to information will show.

import requests
import pandas as pd 
import numpy as np
import json
def 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 response
def  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:UserszhangfengDesktop15 days the weather.xlsx",index = None)
    return weather_data
if __name__ == '__main__':
    print("Please enter the corresponding city")
    web_data = get_url(input())
    weather_data = extract_data(web_data)

部分结果如下图:
在这里插入图片描述

Data interface instance

The use of data interface in the daily learning may be less,Data interface of application scenarios in most case, it is used in the case of a company's internal data to be obtained more,So general rarely seen,Shown here encountered in the work of the use of two data interface,Considering work,The code belongs to the sample,并不能调用.You can refer to the call and to standardize.

POSTRequested data interface
# Sales status query
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']  # Convenient post-processing change column names
    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
GETRequested data interface
### 库存数据查询
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 warehouse storage":overseas_stock,
       "Domestic large warehouse":china_stock
       }
    ad_data=pd.DataFrame(c)
    return ad_data

几种常见API调用实例

百度AI相关API

百度APIIs above the market more matureAPI服务,During the sophomore year because of the need to use some text tagging and image tagging work to understand the baiduAPI,避免了重复造轮子,当时百度API的使用比较复杂,Reference documentation many non-standard,Before written a similar baiduAPICall is extremely unstable,But recently looked at the baiduAPI参考文档,Found that the current call is very simple.
By installing the baidu developmentAPI第三方包,直接利用PythonThe switch pass arguments can be very easy to use.Show a specific use in here,Corresponding to install a third-party library refer to official document.

'''
第三方包名称: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 = "I haven't eat"
# 调用文本纠错 
client.ecnet(text)

在这里插入图片描述

百度地图API

这个APIAt that time in order to design a system of recommended the introduction of latitude and longitude conversion address,Such computing brings great convenience for the data,But for the average person text address latitude and longitude information more intuitive than,然后结合PythonA third-party packages implement the longitude and latitude calculated relative distance between two addresses.

# https://lbsyun.baidu.com/
# 计算校验SN(百度APIDocumentation need this step)
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("Enter the location of the
")  # "广州市天河区"
ak = "ak1111" # With reference to your application
sk = "sk111111" # With reference to your application
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}This position is probably{1}".format(location, distance))

在这里插入图片描述

有道API

在网上查阅了很多API,前面介绍的几种API,They carry the request parameter information is relatively simple,Call request do not have what distinction and realization,Here to find a relatively more request parametersAPI,This relativelyAPIData to payAPI,Its safety as well as concrete implementation is relatively complex,But more suitable for commercial.The following can be simple to see.

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    
# 当文本长度大于20时,进行特殊处理
elif(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])   # 获取翻译内容

翻译后的结果:你好!
这个APICall some of the cited several commercial real to set such as security authentication information,比如uuid、sign、timestamp,这几个在APIIn the call is trite concepts,是比较全面的.下面简单介绍一下.

uuid

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

sign

sign:Of the general in order to prevent malicious caught,Through digital signatures and other guaranteeAPI接口的安全性.In order to prevent the information sent by string change,The sender by some field elements according to certain rules sorted,In into the key,Sent via encryption mechanism,When the receiver receive the request need to verify whether the information has been tampered with,Also need to the corresponding fields in the same rules generated attestationsign,Then is the background to receive compare,Whether can find information are list.In the example above USEShashlib.sha256()To randomly generated a key,最后使用.hexdigest()Returns the final key.
curtime:引入一个时间戳参数,保证接口仅在一分钟内有效,需要和客户端时间保持一致.避免重复访问.

有道API的signStructure can be to the corresponding官方文档查看一下.因为每种APIThe signature of the general construction has a lot of is not the same.So general is given priority to with the official document,For the principle of the signature here,Individual is just an idea,有想详细了解,Can learn their access to information.

常用API分享

The above Outlines the severalAPI的使用,但各有不同,But are based on the structureHTTPRequest according to certain rules to implement,The following supplement some can try also useAPI地址,It can be used in the common data acquisition and learning reference.网上的API特别多,Here are just a few,Are interested you can go to understand,Then use can share out message comments section.
1.聚合数据
2.百度大脑
3.apishop
4.百度地图
5.腾讯地图
6.有道翻译

总结

Recently discovered that actually blog for a long time didn't update,May be because their lazy and ability is limited don't know what to write,But found in the first half of the work and life,Writing is actually a good thing,Can put their ideas down,Although may be very immature,But for your growth is helpful,Write more also cooked,And now fast type of Internet,Let's interrupt a lot of thinking,Especially the discovery after work,Normal or want to think more,多写作,Can also precipitate yourself,Subsequent will gradually update the blog,But the frequency may not be very high,But will slowly add their own thinking and logic, and some more inductive,Because before blog so immature ha ha ha.Time is also a growing,Then I am a rookie in the data analysis and data mining,Problems may arise in the blog,希望大家积极指出,然后相互学习.

最后

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小.自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担.

小编已加密:aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==出于安全原因,我们把网站通过base64编码了,大家可以通过base64解码把网址获取下来.

copyright notice
author[web15286201346],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/252/202209090321180196.html

Random recommended