[Data science basics] Data acquisition from API

Information acquisition technology is the basis of data science. That study.

To get data from API with python and display it

First of all, let's try to bring data from API by learning how to bring data for science.

First of all, what is API

What is API

A convention that defines the procedure and data format for calling and using the functions of a certain computer program (software) and the data to be managed from another external program.

If you ask someone who has data (such as Twitter) to get data from the outside (such as tweeting about python), it will return the data, but it seems like a rule for that.

I'm going to use any API, so let's get some data from where. I'm hungry, so I like rice. → Rice web service ... → yelp.

How to use Yelp API

Documentation is here (https://www.yelp.com.au/developers/documentation).

Yelp's API Console (https://www.yelp.com.au/developers/api_console) is convenient and you can see how it works without looking too much documentation. For the time being, let's write a program that will print 10 restaurants by location and keyword. It seems that the parameters `` `termand location``` correspond to each. (While looking at the document)

~~ It's getting awkward to proceed while explaining ~~, so I'll show you the code below.

It was rauth that seemed to be convenient for handling OAuth requests, so I used it.

find_restaurant.py


import urllib2, sys
import rauth
import ConfigParser

inifile = ConfigParser.ConfigParser()
inifile.read("config/secrets.ini")

def search_params(term, location):
  params = {}
  params["term"] = term
  params["location"] = location
  params["limit"] = "10"
  return params

def get_restaurants(params):
  consumer_key = inifile.get("yelp", "consumer_key")
  consumer_secret = inifile.get("yelp", "consumer_secret")
  access_token = inifile.get("yelp", "access_token")
  access_token_secret = inifile.get("yelp", "access_token_secret")

  session = rauth.OAuth1Session(
      consumer_key = consumer_key,
      consumer_secret = consumer_secret,
      access_token = access_token,
      access_token_secret = access_token_secret)

  request = session.get("http://api.yelp.com/v2/search", params=params)

  data = request.json()
  session.close()

  return data

def main():
  term = sys.argv[1]
  location = sys.argv[2]
  params = search_params(term, location)
  response = get_restaurants(params)
  restaurants_info = response["businesses"]
  restaurant_names = []
  for restaurant_info in restaurants_info:
    restaurant_name = restaurant_info["name"]
    print restaurant_name

if __name__ == "__main__":
  main()

Wow, very simple. (I didn't know python grammar at all, so I forgot `:` at first.) I just made a method to create parameters and a method to get data by hitting the API, and used them. `" business "` and `` `name``` are the json key names of the response, respectively.

Especially for me, a python beginner, the last two lines,

if __name__ == "__main__":
  main()

Check this out. (Why do __name __ == "main") I also checked configparser. (This or something)

Well, the story was off, but I got back to the main subject and the code was completed, so on the console,

python find_restaurant.py food tokyo

so

Yamato sushi
Numazu Port Shinjuku West Exit
Tonki
Full Monty
Maruyama Coffee Nishi Azabu
AW Elements Roppongi Hills store
Harajuku Gyozaro
The chef's table
Ichiran

Came out. Umm. Information acquisition from API completed! No. (Click here for git)

Well, this alone is rather normal, and it's too easy. Even if you look at the result, it's only about "Oh, Ichiran!" .. So I'll twist it a little more. → Continued from the next time onwards. I will paste the link when updating.


Recommended Posts

[Data science basics] Data acquisition from API
[Basics of data science] Collecting data from RSS with python
Data acquisition using python googlemap api
Python: Reading JSON data from web API
Acquisition of plant growth data Acquisition of data from sensors
[Data science basics] I tried saving from csv to mysql with python
Learn data science
Data acquisition from analytics API with Google API Client for python Part 2 Web application
Extract data from S3
Challenge 100 data science knocks
OPT data science competition
Get data from analytics API with Google API Client for python
Meteorology x Python ~ From weather data acquisition to spectrum analysis ~
Get structural data from CHEMBLID
Data science 100 knock commentary (P021 ~ 040)
python-fitbit data acquisition query summary
Export 3D data from QGIS
Data science 100 knock commentary (P041 ~ 060)
Data science 100 knock commentary (P081 ~ 100)
Try "100 knocks on data science" ①
Infra_ Data Science Course Output
Stock price data acquisition tips
Use e-Stat API from Python
Create an API that returns data from a model using turicreate