**Table of contents of this article
**1. Foreword
2. Call the Zhejiang Data Open Platform API to obtain data
(1) Process of obtaining data by API
(2) HTTP request
(3) API parameters
(4) Use the request library to obtain API data
3. Call Baidu Universal Translation API
**4. **Summary
This article has a total of 8914 words and takes about 23 minutes to read. Corrections are welcome!
Introduction to Part 1
The full name of API is “Application Programming Interface
“, which is “Application Programming Interface
“. It is a set of rules and regulations that define how different software components communicate with each other. Protocols, different software systems can exchange data and functions through APIs to achieve specific mission goals. Through APIs, we can take advantage of the functionality of other software systems without having to understand in detail how they are implemented internally.
In the following two articles, we introduce how to use Python to call the geographical service API provided by AutoNavi and the image recognition OCR API provided by Baidu, so that text addresses and longitude and latitude can be freely converted with the help of geocoding and reverse geocoding. , and can also read unreadable PDF or tables in images into Excel through OCR technology.
Data Governance | Obtaining latitude, longitude and administrative divisions based on address – the wonderful use of API
Data Governance | Are you still identifying forms manually? Python calls Baidu OCR API quickly and accurately
So why introduce the API again? Our purpose is to introduce you to the relevant concepts of API and how to use the requests
package in Python to call the API to obtain data or implement corresponding functions, which is more universal. Below we will use two Examples expand to introduce the content of this article.
This tutorial is based on pandas version 1.5.3.
All Python code in this article was written in the integrated development environment Visual Studio Code (VScode) using the interactive development environment Jupyter Notebook.
Part2 calls Zhejiang·Data Open Platform API to obtain data
We first introduce how to call the API to obtain data. Zhejiang·Data Open Platform
is a government platform that provides users with free data interface services and realizes data acquisition, development and utilization through interface docking. The platform provides each data All provide 3 interfaces, including total interface, paging interface and update time query interface, which are used to query the total number of data contained in the data set, retrieve data by paging, and determine whether the current data has been updated. In this example, we use the “Zhejiang Grain Enterprise Credit Evaluation Result Information
” interface provided by Zhejiang·Data Open Platform
to obtain Credit evaluation data of grain enterprises in Zhejiang ProvinceThis interface is released by the Grain and Materials Bureau of Zhejiang Province, and we use the paging interface.
It should be noted that before we actually use an API, the first and most important step is to carefully read the relevant documentation of the API to understand its functions, parameters and other important information. Then comes the subsequent process.
1API data acquisition process
As mentioned above, API is a set of specifications that define the interaction between different software components. During the interaction, API can use different communication protocols, the most commonly used of which is HTTP. HTTP (“Hypertext Transfer Protocol”) is a protocol for sending and receiving hypertext over the Internet, whichprovides a reliable way to send requests and receive responses , imagine that you use a browser to visit a website. When you enter the URL in the browser and press the Enter key, the browser will send an HTTP request to the server. This request will tell the server which web page or resource you want to browse; When the server receives this request, it will process the request and generate a response. The response contains the requested content, such as the HTML code of the web page, images and other resources. The server sends the response back to the browser, and the browser parses the response and displays the content on the screen. This communication protocol has broad support and ease of use.
The following is the process of calling the API to obtain data:
GET request and POST request are the two most common request methods in the HTTP protocol, which are used for data interaction between the client and the server. The main differences between the two are transmission security, transmission method, data Several aspects of length and data caching (due to space reasons, we will not introduce them in detail here).
When calling the API, we should choose to use a GET request or a POST request based on specific needs and security considerations. Generally, if you only need to obtain a small amount of data and do not involve sensitive information, you can use the GET request. If you need to submit data to the server, or the amount of data is large, you can use the POST request.
2HTTP request
HTTP request is a data format under the HTTP protocol. It is used to send requests to the server. It usually consists of three parts: request line, request header and request body. The request header and request body are separated by blank lines, in which each Some of the information included is as follows:
-
Request Line: includes request method (GET request, POST request, etc.), requested URL and protocol version.
-
Request Headers: Includes some additional information about the request, such as User-Agent (user agent, identifying the client type of the request, etc.), Content-Type (specifying the type of data sent in the request body), etc.
-
Request Body: An optional component of an HTTP request, used to pass parameters or data required for the request to the server, such as form data, JSON data, etc.
The GET request passes the parameters to the server through the query string of the URL, which means that the parameters will be appended to the end of the URL, while the POST request puts the parameters in the request body and passes them to the server, so usually the request body of the GET request is empty. , the request body of the POST request is not empty.
3API parameters
Usually when calling an API, you need to pay attention to three parameters: public parameters, request parameters, and response parameters. The information contained in each part is as follows:
-
Public parameters: Common parameters often used when calling APIs. They are used to identify the identity, version, signature and other information of the request. These parameters will not change as the API changes when calling different APIs.
-
Request parameters: Parameters that need to be provided when calling a specific API to describe the specific content of the request (such as passing query conditions, paging information, etc.). Whether these parameters are required or optional depends on the design of the specific API.
-
Response parameters: The information returned after a successful API call, usually including the status code of the API call result, error information, returned data, etc. Depending on the design of the API, the content and format of the response parameters may vary.
Public parameters and request parameters are used to initiate API requests, while response parameters are the results returned by the API. It should be noted that different APIs may have different parameter requirements.
In this example, we need to obtain the credit evaluation data of grain enterprises in Zhejiang Province. According to the introduction of the paging data acquisition interface, the parameters of the API design are as follows:
Parameter name | Type | Description |
---|---|---|
appsecret | String | User application identification code, obtained through application |
pageNum | Int | Number of pages |
pageSize | Int | Number of pages per page (not more than 200) |
The parameters appsecret
in the above table are public parameters, and pageNum
and pageSize
are request parameters.
Parameter name | Type | Description |
---|---|---|
status | Int | 0 represents failure, 1 represents success |
msg | String | Return information |
data | JSON | Return data |
The three parameters in the above table are response parameters, used to return the results of the request. We can extract the corresponding content from the returned response information only based on the names of the parameters. Of course, before using the Zhejiang Provincial Data Open Platform interface, you need to complete user registration and log in to the platform to create a data application and improve the application information. After the application information is saved, you can apply for the interface. After the interface application is approved, you can proceed with the data interface docking. Work, please see the figure below for the operation process: More detailed registration and application For the process, please refer to the official
Interface Service User Manual
, which will not be described again in this article.
4Use the requests library to obtain API data
If you know about web crawling or API interaction, then you should be familiar with the requests library. requests is a Python library commonly used to send HTTP requests and process responses. Among them, requests.get()
and requests.post()
are two commonly used functions, which are used to send GET requests and POST requests respectively.
The basic usage of the function requests.get()
is as follows:
import requests response = requests.get(url='https://api.example.com/data')
The above code will send a GET request to https://api.example.com/data
(that is, the parameter url), and save the response result in the variable response
. In addition, this function also has a commonly used optional parameter params
for passing query parameters. The parameters will be automatically added to the URL. The code is as follows:
params_value = {'key1':'value1', 'key2':'value2'} response = requests.get('https://api.example.com/data\ ', params=params_value)
The above code will send a GET request to https://api.example.com/data?key1=value1 & amp;key2=value2
. In addition to the parameters url
and params
, the function requests.get()
also has some other optional parameters, such as the parameter headers
code> is used to set the request header, timeout
is used to set the request timeout, cookies
is used to set the cookie value of the request, etc.
The basic usage of the function requests.post()
is similar, except for the parameter url
, the optional parameter data
of this function is used to pass the request Data, the code is as follows:
import requests params_value = {'key1':'value1', 'key2':'value2'} response = requests.post('https://api.example.com/ data', data=params_value)
The above code will send a POST request to https://api.example.com/data
and send params_value
to the server as the request data. Of course, you can also use the parameter json
to send JSON data. If the parameter json=params_value
is set, the data will be automatically converted into JSON format and sent to the server. In addition, the function requests.post()
code> also has other optional parameters, which will not be introduced here.
Below we will use the functions requests.get()
and requests.post()
in the requests library to obtain the credit evaluation data of grain enterprises in Zhejiang Province. The following is the Python code:
import requests import pandas as pd # GET request def data_get(pageNum, pageSize, appsecret, url): ''' pageNum: page number pageSize: number of pages per page (not more than 200) appsecret: application identification code (obtained by application) url: api interface''' # Edit query parameters in dictionary form parameters = {'pageNum':pageNum, 'pageSize':pageSize, 'appsecret':appsecret} # Send GET request, returns a response object containing server response information response = requests.get(url = url, params = parameters) # An HTTP response status code of 200 indicates that the request is successful and the server successfully processed the request if response.status_code == 200: # The status in the response information is 1, indicating that the data was successfully obtained if response.json()['status'] == 1: ## Extract the data data returned in the response result and convert it into a dataframe data = pd.DataFrame( response.json()['data']) else: # The status in the response information is not 1, which means that the data acquisition failed, and the reason needs to be further checked print(response.json()) else: # The HTTP response status code is not 1 200, the prompt "URL did not respond to the request normally" raise Exception('URL did not respond to the request normally') return data data = data_get(pageNum = 1, pageSize = 200, appsecret = 'Applied APP identification code', url = 'http://data.zjzwfw.gov.cn/jdop_front/interfaces/cata_18444/get_data.do') data
# POST request def data_post(pageNum, pageSize, appsecret, url): # Edit the request body in dictionary form data_value = {'pageNum':pageNum, 'pageSize':pageSize, 'appsecret': appsecret} # Send a POST request and return a response object containing server response information response = requests.post(url = url, data = data_value) if response.status_code == 200: # The status in the response information is 1, indicating that the data was successfully obtained if response.json()['status'] == 1: data = pd.DataFrame(response.json()['data']) else: print(response.json()) else: raise Exception ('URL did not respond to the request normally') return data data = data_post(pageNum = 1, pageSize = 200, appsecret = 'Applied APP identification code', url = 'http://data.zjzwfw.gov .cn/jdop_front/interfaces/cata_18444/get_data.do') data # The result is the same as above
Using the functions requests.get()
and requests.post()
yields the same result. The data includes the company name, unified social credit code, score and There are four fields of level, a total of 107 pieces of data, as follows:
Part3 calls Baidu Universal Translation API
Baidu Translation Open Platform
is a platform for Baidu Translation to provide open services to developers. The universal translation API is one of the services provided by the platform. The universal translation API provides multi-lingual translation services to the outside world through the HTTP interface. We only need to pass in the content to be translated and specify the source language and target language to be translated to get the desired translation results. Here we will introduce how Call this API.
According to the official universal translation API access document, calling this API supports GET or POST. If you need to use POST, you need to specify the Content-Type as application/x-www-form-urlencoded. First, let’s take a look at the input parameters required by the universal translation API, as shown in the following table:
Parameter name | Description |
---|---|
q | Content to be translated, UTF-8 encoding, no longer than 6000 bytes |
from | Translation source language |
to | Translation target language |
appid | Platform assigned application’s Identifier |
salt | Random number (salt value) |
sign | appid + q + salt + 32-bit lowercase signature obtained after doing MD5 on the key |
In order to ensure the security of calling the API, the universal translation API requires the parameter sign
to be used to sign the request. The signature consists of four parts: appid assigned by the platform, content to be translated q, random value salt and key. The generation method is: concatenate the appid, q, salt, and key into a string in order, and then perform MD5 calculation on the string to obtain a 32-bit lowercase sign.
After successfully calling the API, a response result in JSON format is returned, which contains the following output parameters:
The official has more detailed descriptions of input parameters and output parameters, you can learn more by yourself
Parameter name | Type | Description |
---|---|---|
from | String | Translate source language |
to | String | Translate target language |
trans_result | Array | Translation results, including original content (src) and translation results (dst) |
error_code | Int | Error code, only displayed when an error occurs. For detailed meaning and solution, see the Universal Translation API Access Document |
Next we will call the universal translation API. The implementation code is as follows:
import requests import random from hashlib import md5 # GET request def trans_Baidu_GET(query, APPID, APPKEY, fromlanguage = 'auto', tolanguage = 'en', action = 0): ''' query: content to be translated APPID: applied APP ID APPKEY: applied key fromlanguage: language to be translated; 'auto' means automatic recognition tolanguage: translation target language; 'zh' means Chinese* For language codes, see: https ://api.fanyi.baidu.com/doc/21 action: 1: Use custom terms to intervene in the API; 0: Do not use custom terms to intervene in the API ''' # Define a function, which is used to perform MD5 and Convert the hash value to hexadecimal def make_md5(s, encoding = 'utf-8'): return md5(s.encode(encoding)).hexdigest() # Take a random number Salt in the range of 32768 to 65536 = random.randint(32768, 65536) # Use APPID, request query, random number and key to form a signature Sign = make_md5(APPID + query + str(Salt) + APPKEY) # Send the requested URL url = 'https:/ /fanyi-api.baidu.com/api/trans/vip/translate' # Edit query parameters in dictionary form parameters = {'appid': APPID, 'q': query, 'from': fromlanguage, 'to': tolanguage, 'salt': Salt, 'sign': Sign} # Return the response information and extract the translation results in the response response = requests.get(url, params = parameters) result_list = response.json()['trans_result'] ## Extract the translated content (dst) in the translation result result = '\\ '.join(item['dst'] for item in result_list) return result # POST request def trans_Baidu_POST(query, APPID, APPKEY, fromlanguage = 'auto', tolanguage = 'zh', action = 0): # Define a function to perform MD5 and hash the value Convert to hexadecimal def make_md5(s, encoding = 'utf-8'): return md5(s.encode(encoding)).hexdigest() Salt = random.randint(32768, 65536) Sign = make_md5(APPID + query + str(Salt) + APPKEY) # URL for sending universal translation request url = 'https://fanyi-api.baidu.com/api/trans/vip/translate' # Specify according to the API access document Content-Type is application/x-www-form-urlencoded headers = {'Content-Type': 'application/x-www-form-urlencoded'} parameters = {'appid': APPID, \ 'q': query, 'from': fromlanguage, 'to': tolanguage, 'salt': Salt, 'sign': Sign} # Save the response and extract the translation results in the response response = requests.post(url, params = parameters, headers = headers) result_list = response.json()['trans_result'] # Extract the translated content (dst) in the translation result result = '\\ \ '.join(item['dst'] for item in result_list) return result
In the above code, we define the functions trans_Baidu_GET()
and trans_Baidu_POST()
, which call the universal translation API using GET request and POST request respectively. Their functions are the same. When calling the function, we only need to enter the original text to be translated (default automatically identifies the language), APPID and key, and then we can get the translation result (default English). Of course, if you want to translate to other languages or Modifying the target language of translation can be achieved by specifying the parameters fromlanguage
and tolanguage
.
Of course, we can also translate multiple statements in batches. We only need to separate each statement with a newline character \\
. At this time, the translation result also uses a newline character
\\
output. Now we call the above function, the code is as follows:
APPID = 'Assigned APPID' APPKEY = 'Key corresponding to APPID' # GET request answer1 = trans_Baidu_GET('Become a member of the Enterprise Research·Social Science Big Data Platform and use the most exclusive data to learn The most practical Python, draw the coolest pictures!', APPID, APPKEY) answer1 ''' Become a member of the Enterprise Research Social Science Big Data Platform, use the most exclusive data, learn the most practical Python, and draw the coolest pictures! ''' # POST request answer2 = trans_Baidu_POST('Become a member of Enterprise Research·Social Science Big Data Platform,\\ Use the most exclusive data,\\ Learn the most practical Python, \\ Draw the coolest picture!', APPID, APPKEY) answer2 ''' Becoming a member of the Enterprise Research · Social Science Big Data Platform, Using the most exclusive data, Learn the most practical Python, Draw the coolest picture! '''
Part4 Summary
This article introduces how API can access external data and functions. It also briefly introduces the HTTP protocol and two commonly used request methods – GET request and POST request. Then it demonstrates how to use requests in Python through two examples. The library implements API calls and parses the returned response results to obtain the corresponding content. It also shows you the similarities and differences between the two request methods. I hope it will be helpful to everyone.
In the next article, we will use the function of calling the API in this issue as the basis to introduce how to use OpenAI’s API to implement the continuous conversation function of chatgpt to help our data processing and analysis! Stay tuned!
———————————END——————- ——–
Digression
Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.
CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)
1. Python learning routes in all directions
The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.
2. Essential development tools for Python
The tools have been organized for you, and you can get started directly after installation!
3. Latest Python study notes
When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.
4. Python video collection
Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.
5. Practical cases
What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.
6. Interview Guide
Resume template
CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)
If there is any infringement, please contact us for deletion.