Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error zomato scraping data
#1
hey, i just learned about zomato data scraping. then I practice using one of the codes on github.

import requests import csv with open("output_restaurant.csv", "a", newline='') as fp: wr = csv.writer(fp, dialect='excel') url = "https://developers.zomato.com/api/v2.1/search?entity_id=4&entity_type=city&q=Bangalore&start=80" header = {"Accept": "application/json", "user-key": "API_KEY", "User-agent": "curl/7.43.0"} resp = requests.get(url, headers=header).json() for i in range(0, 20): rest = resp['restaurants'][i] res_id = rest['restaurant']['id'] name = rest['restaurant']['name'] locality = rest['restaurant']['location']['locality'] cuisines = rest['restaurant']['cuisines'] average_cost_for_two = rest['restaurant']['average_cost_for_two'] rating = rest['restaurant']['user_rating']['aggregate_rating'] votes = rest['restaurant']['user_rating']['votes'] list_ = [res_id, name, locality, cuisines, average_cost_for_two, rating, votes] wr.writerow(list_)
when run the first time an error like this appears
Output:
Traceback (most recent call last): File "Zomato_API.py", line 10, in <module> rest = resp['restaurants'][i] KeyError: 'restaurants'
then some time the error changes to
Output:
Traceback (most recent call last): File "Zomato_API.py", line 10, in <module> rest = resp['restaurants'][i] IndexError: list index out of range
from the error, what is the way to fix it?
Reply
#2
You could replace the for i in range... loop by
if 'restaurants' in resp: for i, res in enumerate(resp['restaurants']): ...
Reply
#3
(Jun-22-2020, 08:09 AM)Gribouillis Wrote: You could replace the for i in range... loop by
if 'restaurants' in resp: for i, res in enumerate(resp['restaurants']): ...

i try this and problem solved, thanks bro.
can you explain why 'for i in range' error in this code ?
Reply
#4
Because the range uses all the integers 0, 1, ..., 19. If the list rest['restaurants'] has less than 20 elements, the lookup will fail.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to scraping data from dinamic site sergio21124444 2 2,230 Nov-08-2023, 12:43 PM
Last Post: sergio21124444
  I am scraping a web page but got an Error Sarmad54 3 3,117 Mar-02-2023, 08:20 PM
Last Post: Sarmad54
  What is the error of not being able to pull data in this code? i didn't see an error? TestPerson 2 2,550 Sep-30-2022, 02:36 PM
Last Post: DeaD_EyE
  Scraping data from table into existing dataframe vincer58 1 3,931 Jan-09-2022, 05:15 PM
Last Post: vincer58
  Web scraping data Mike_Eddy 2 3,827 Jul-03-2021, 05:49 PM
Last Post: Mike_Eddy
  Scraping lender data from Ren Ren Dai website using Python. I will pay for that 200$ Hafedh_2021 1 3,892 May-18-2021, 08:41 PM
Last Post: snippsat
  Scraping a page with log in data (security, proxies) iamaghost 0 3,921 Mar-27-2021, 02:56 PM
Last Post: iamaghost
  Scraping Data from Singapore Turf Club singaporeman 2 3,881 Dec-15-2020, 01:28 PM
Last Post: MrBitPythoner
Thumbs Up Issue facing while scraping the data from different websites in single script. Balamani 1 3,458 Oct-20-2020, 09:56 AM
Last Post: Larz60+
  error in code web scraping alexisbrunaux 5 5,901 Aug-19-2020, 02:31 AM
Last Post: alexisbrunaux

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.