Started web scraping and I've been trying to get the rating off this yelp review.
my code:
rating = review.find('img', attrs={'class': 'offscreen__373c0__1KofL'}).get('alt')
rating = float(re.findall('\d+', rating)[0])
print(rating)
result:
Traceback (most recent call last):
File "/Users/joseochoa/PycharmProjects/webbscraper/Tutorial_scraper.py", line 40, in <module>
rating = float(re.findall('\d+', rating)[0])
IndexError: list index out of range
Any help or suggestions would be appreciated. Thank you.
It seems that re.findall('\d+', rating)
returns empty list.
For this reason when you try to get the first element of the list you have this error.
You can modify your code like this:
rating = review.find('img', attrs={'class': 'offscreen__373c0__1KofL'}).get('alt')
rating_list = re.findall('\d+', rating)
if len(rating_list) > 0:
rating_float = float(rating_list[0])
print(rating_float)
else:
print("No matches have been found")
# manage with specific actions if you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.