Reputation: 22440
I've written a script in python to scrape some item names
along with review texts
and reviewers
connected to each item name from a webpage using their api. The thing is my below script can do those things partially. I need to do those in an organized manner.
For example, in each item name
there are multiple review texts
and reviewer names
connected to it. I wish to get them along the columns like:
Name
review text
reviewer
review text
reviewer
-----
Basically, I can't get the idea how to make use of the already defined for loop
in the right way within my script. Lastly, there are few item names
which do not have any reviews
or reviewers
, so the code breaks when it doesn't find any reviews
and so.
This s my approach so far:
import requests
url = "https://eatstreet.com/api/v2/restaurants/{}?yelp_site="
res = requests.get("https://eatstreet.com/api/v2/locales/madison-wi/restaurants")
for item in res.json():
itemid = item['id']
req = requests.get(url.format(itemid))
name = req.json()['name']
for texualreviews in req.json()['yelpReviews']:
reviews = texualreviews['message']
reviewer = texualreviews['reviewerName']
print(f'{name}\n{reviews}\n{reviewer}\n')
If I use print statement outside the for loop, It only gives me a single review
and reviewer
.
Any help to fix that will be highly appreciated.
Upvotes: 0
Views: 48
Reputation: 1402
You need to append the review and a reviewer name to an array to display as you wish.
Try the following code.
review_data = dict()
review_data['name'] = req.json()['name']
review_data['reviews'] = []
for texualreviews in req.json()['yelpReviews']:
review_sub_data = {'review': texualreviews['message'], 'reviewer': texualreviews['reviewerName']}
review_data['reviews'].append(review_sub_data)
#O/P {'name': 'xxx', 'reviews':[{'review':'xxx', 'reviewer': 'xxx'}, {'review':'xxx', 'reviewer': 'xxx'}]}
Hope this helps!
Upvotes: 1