当前位置:网站首页>Crawler series: using API
Crawler series: using API
2022-06-24 02:31:00 【Empress Dowager】
Application programming interface (Application Programming Interface, API) Usefulness : It provides convenient and friendly interfaces for different applications . Different developers use different architectures , Even writing software in different languages has no problem —— because API The purpose of design is to become a communication language , Let different software share information .
Although different software applications have their own characteristics API, but “API” Often seen as “ Web applications API”, In general , Programmers can use HTTP Agreement to API Make a request to get some information ,API Will use XML(eXtensible Markup Language, Extensible markup language ) or JSON (Javascript Object Notation,Javascript Objects represent ) The format returns the information of the server response . Although most API Still in use XML, however JSON It is rapidly becoming the mainstream choice of data coding format .
Although this out of the box interface obtains pre packaged information , It seems that it has nothing to do with reptiles , But this view is only half right . Although most people don't usually use API As network data collection , But in fact, many of the technologies used by both ( It's all about sending HTTP request ) And the results ( It's all about getting information ) Not so much , The two are often complementary .
for example , You might put web crawlers and API Combine the information obtained , Because such information may be more meaningful .
## API summary
although API Not everywhere , But you can go through API Get a lot of useful information from . If you're interested in music , There is a song name 、 singer 、 Album Information API.
Here is API An example of a call :
https://api.bigdatacloud.net/data/ip-geolocation-full?ip=27.30.84.181&localityLanguage=zh&key=bee73355d8ad4821a1c393c545e7f0
You enter the above URL through the browser , You can initiate a simple API call , give the result as follows :
{
"ip": "199.21.99.90",
"localityLanguageRequested": "zh",
"isReachableGlobally": true,
"country": {
"isoAlpha2": "US",
"isoAlpha3": "USA",
"m49Code": 840,
"name": " The United States ",
"isoName": "United States of America (the)",
"isoNameFull": "the United States of America",
"isoAdminLanguages": [
{
"isoAlpha3": "eng",
"isoAlpha2": "en",
"isoName": "English",
"nativeName": "English"
}
],
"unRegion": "Europe and Northern America/Northern America",
"currency": {
"numericCode": 840,
"code": "USD",
"name": "US Dollar",
"minorUnits": 2
},
"wbRegion": {
"id": "NAC",
"iso2Code": "XU",
"value": "North America"
},
"wbIncomeLevel": {
"id": "HIC",
"iso2Code": "XD",
"value": "High income"
},
"callingCode": "1",
"countryFlagEmoji": "
Some of the results are excerpted above .
You might think , This is not to enter a web address in the browser window , Then press enter to get ( It's just JSON Format ) Information ? Exactly? API What's the difference between calling and ordinary website access ? If you don't think about API The name on the tall , In fact, there is no difference between the two .API Can pass HTTP Protocol Download File , and URL The protocol for accessing the website to obtain data is the same . It can realize all the things that are done online now .API The name API Why not call it a website , Actually, first of all API Have very strict grammar , secondly API use JSON or XML Format represents data , instead of HTML Format .
The above explains API Related concepts of , obtain API The data of , It's usually JSON data , Let's see Python How to parse in JSON data .
## analysis JSON data
Let's take the example just now , See how to pass API Acquired JSON Data to get the desired results :
import requests
class ScrapeAPI(object):
def __init__(self):
self._api_url = 'https://api.bigdatacloud.net/data/ip-geolocation-full?ip=27.30.84.181&localityLanguage=zh&key=bee73355d8ad4821a1c19345e7f0'
def get_geolocation(self):
init_session = requests.Session()
response = init_session.get(self._api_url)
json_result = response.json()
get_country = json_result['country']['name']
get_location = json_result['location']
get_region = get_location['isoPrincipalSubdivision']
get_city = get_location['city']
get_locality_name = get_location['localityName']
area = f' At present IP Country :{get_country}, region :{get_region}, City :{get_city}, Division :{get_locality_name}'
print(area)
if __name__ == '__main__':
ScrapeAPI().get_geolocation()
The code above uses requests Library pair JSON Data analysis , Run the code to get the results we need .
That's what today is about API Related content .
边栏推荐
- Offline store + online mall, why do you want to be an online mall
- UNIX command encyclopedia, common commands are here, work must!
- How to build a website? These things should be paid attention to
- 5g Gigabit router dual band industrial grade
- Layout use case
- IPhone sending SMS implementation
- [expense center] demand & problem feedback week is coming! Feedback wins a good gift!
- How to bind EIP to access public network in tke cluster fixed IP mode pod
- Micro850 Simulator
- Centeros environment setup
猜你喜欢

application. Yaml configuring multiple running environments
Cloudpods golang practice

Advanced BOM tool intelligent packaging function

163 mailbox login portal display, enterprise mailbox computer version login portal

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

Leetcode969: pancake sorting (medium, dynamic programming)

How to fill in and register e-mail, and open mass mailing software for free

Introduction to development model + test model

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
随机推荐
Designing complex messaging systems using bridging patterns
Micro850 Simulator
The blue screen will be displayed at a remote location, and the error code kmode will be reported_ EXCEPTION_ NOT_ Handled, the DMP file has the keyword cdd dll
Internal reasons for cloud desktop unable to connect to the server and external reasons for cloud desktop connection failure
Vivo global mall: design and practice of commodity system architecture
What is a private VLAN? Eight part essay with pictures and texts.
Evaluation index of machine learning model
What are the general contents of the enterprise website construction scheme
How to design and make ppt gradient effect?
Some tips for using uitextview
Cloud game cannot select a server cloud game server fees
The United States offered 10million yuan to hunt down blackmail hackers and the energy industry became the "hardest hit" of phishing attacks | global network security hotspot
[Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
What is a region name? Can a territory name be used for trademark registration?
Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
How to apply for top-level domain names? What are the types of top-level domain names?
Opengl: how to use shader to convert RGBA to nv21 image format? (open source for the first time in the whole network)
Uiscrollview add gestures show and hide keyboard
Operation and maintenance platform tcapulusdb transaction management