Оптимізоване завантаження даних із pandas
Amany Mahfouz
Instructor



requests.get() для отримання даних з URL
requests.get(url_string) для отримання даних з URLparams: приймає словник параметрів і значень для налаштування запиту до APIheaders: приймає словник; можна використати для автентифікації користувача в APIresponse з даними та метаданимиresponse.json() повертає лише JSON-даніresponse.json() повертає словникread_json() очікує рядки, не словникиpd.DataFrame()read_json() спричинить помилку!





import requests import pandas as pdapi_url = "https://api.yelp.com/v3/businesses/search"# Set up parameter dictionary according to documentation params = {"term": "bookstore", "location": "San Francisco"}# Set up header dictionary w/ API key according to documentation headers = {"Authorization": "Bearer {}".format(api_key)}# Call the API response = requests.get(api_url, params=params, headers=headers)
# Isolate the JSON data from the response object
data = response.json()
print(data)
{'businesses': [{'id': '_rbF2ooLcMRA7Kh8neIr4g', 'alias': 'city-lights-bookstore-san-francisco', 'name': 'City Lights Bookstore', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/VRydkkpVbA3CeVLBKzs2Vw/o.jpg', 'is_closed': False,
# Load businesses data to a dataframe
bookstores = pd.DataFrame(data["businesses"])
print(bookstores.head(2))
alias ... url
0 city-lights-bookstore-san-francisco ... https://www.yelp.com/biz/city-lights-bookstore...
1 alexander-book-company-san-francisco ... https://www.yelp.com/biz/alexander-book-compan...
[2 rows x 16 columns]
Оптимізоване завантаження даних із pandas