Bottom Page

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?
Quote
#2
You could replace the for i in range... loop by
if 'restaurants' in resp:
    for i, res in enumerate(resp['restaurants']):
        ...
Quote
#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 ?
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Web Scraping and data collection. LordDeanUK 6 1,516 Jun-29-2020, 06:04 PM
Last Post: HarleyQuin
  Scraping Data issues TrexKikBut 0 682 Jun-02-2020, 05:49 PM
Last Post: TrexKikBut
  POST request with form data issue web scraping hoff1022 0 301 Apr-12-2020, 03:22 PM
Last Post: hoff1022
  Web scraping error jithin123 0 238 Mar-22-2020, 08:13 PM
Last Post: jithin123
  Scraping a dynamic data-table in python through AJAX request filozofo 0 230 Mar-18-2020, 01:44 PM
Last Post: filozofo
  Web Scraping, Merging two lists and getting data from various dates? AgileAVS 0 237 Feb-07-2020, 04:05 PM
Last Post: AgileAVS
  Web Scraping Error : Not getting expected result adminravi 4 382 Oct-08-2019, 09:53 AM
Last Post: snippsat
  Scraping data from ebay seller page yuvalta 3 2,815 Sep-25-2019, 04:22 AM
Last Post: sandramoraes
  Scraping data saving to DB error with Cursor cubangt 3 582 May-20-2019, 08:30 PM
Last Post: Yoriz
  Python Scraping Error ZenWoR 1 687 Sep-15-2018, 08:23 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 2 Guest(s)