Several friends in the background asked me if I could write another one, and two of them left me a message asking me what Web Api is. Ok, I will explain it today, and use Baidu’s open Api to write a simple question. It is further interesting to learn JSON files.

1. What is Web API

Web APIS are Web application programming interfaces (apis) that contain a wide range of functions, most of which can be accessed through apis (Application programming interfaces).

At present, there are a large number of Web services and applications on the network, which will provide us with a variety of services, such as Yahoo,Google abroad, Baidu, Ali in China, all have a large number of API for us to call

Generally, the returned data is mainly in JSON format (CSV or XML is also acceptable), and most of them need to provide key and secret to ensure their security

And there are some API access is such as illegal ah, express query, according to the number of charges





Today we will write a mini version of go out to ask small procedures, go out to ask the main need to provide:

Weather at your destination

Tourist attraction of destination

Destination cuisine

Destination fun

2. Apply for a Baidu developer account

1). Before calling Baidu Web API, apply for a developer account:

http://lbsyun.baidu.com/apiconsole/key





Then apply for a key





2). Create an app and fill in some information





We can choose the simplest type of wechat applet application and submit it to get a key







3. Weather enquiry procedures

1). Construct the weather query URL





You need to fill in the application key and city name (Either Chinese or Pinyin).

2). Use the Requests module to fetch data from the site

import request

response=requests.get(url)

weather_dicts=response.json()

We converted the JSON data of the site into a dictionary, but the JSON data is very complex and nested in a lot of clumps. To solve this problem, I recommend that you use the pprint module

3). Use the pprint module

Called a beautiful printer in Python, it can be used to generate a beautiful view of data structures. People can be very intuitive and clear to see the structure of the data, very convenient, but also the key values in alphabetical order, it is a necessary home travel ah ~~

pprint(weather_dicts)

4). Display weather information

All the weather information is under the result key weather_dicts (pprint(weather_dicts)), which is a huge list with a dictionary in it. The dictionary contains city names,PM2.5, weather data for the next 4 days and so on.

Thanks to pPrint, otherwise we’d be dizzy. We just need to extract some important information.

weather_data=weather_dicts[‘results’][0][‘weather_data’]

print weather_dicts[‘results’][0][‘currentCity’]

for each_item in weather_data:

print each_item[‘date’]

print each_item[‘temperature’]

print each_item[‘weather’]

print each_item[‘wind’]

Run the program:





Ah ah is not very cool, Shanghai 4 days of weather have come out. We can add a city parameter at the entrance of the program, so that we can get the weather of different parts of the country. For example, if you input Beijing, Guangzhou, Wuhan, Qingdao…

4. Query hot city tourist attractions, food, fun

Now let’s add another function, is to query some information about popular cities, such as where we go to play, want to check some hotels, hotel information, there are those fun, delicious ~~, code is very similar to query the weather

1). Construct the same query Place URL





2). Use the pprint module to print some data structures and then get the information

pprint(places_dicts),type(places_dicts)





Here added some judgment conditions, because there are some hotels or scenic spots do not necessarily have a phone and address, so between the acquisition, the first judge key is not in the dictionary, otherwise it will report an error

3) run the program

Search_place (hot_place=” travel “,city=” Shanghai “)





Input:

Search_ place(hot_place=” hotel “,city=” Shanghai “)

Search_place (hot_place=” delicious “,city=” Shanghai “)

Will return some hotel information and delicious information in Shanghai

In fact, the above code is still rather rough. It is only a primer. Interested students can try it, and use the interface of wechat public account to make a more complete and more fun to ask outside. Today’s article hope to give beginners some inspiration, if what do not understand, you can also leave a message with me to discuss exchanges.