Web Scraping with BeautifulSoup project

Chocolate Scraping with Beautiful Soup

In this project, step 7 is creating a problem for me can somebody help
the link for the same is :https://www.codecademy.com/paths/data-science/tracks/learn-web-scraping/modules/beautiful-soup/projects/chocolate-scraping-with-beautiful-soup

Thank You :slightly_smiling_face:

I am quite stuck here as well…

My code until step 7:

import codecademylib3_seaborn
from bs4 import BeautifulSoup
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

webpage_response = requests.get('https://s3.amazonaws.com/codecademy-content/courses/beautifulsoup/cacao/index.html')

chocolates = webpage_response.content

soup = BeautifulSoup(chocolates, "html.parser")

chocolate_ratings = soup.find_all(attrs={"class": "Rating"})
ratings = []

for r in chocolate_ratings:
  ratings.append([r])

The task is:

Loop through the ratings tags and get the text contained in each one. Add it to the ratings list.

As you do this, convert the rating to a float, so that the ratings list will be numerical. This should help with calculations later.

Hint

The first element of your tags list probably contains the header string "Ratings" , so we probably should leave this off the list. Start your loop at element 1 of the list instead.

You can cast a string x to a float with this syntax:

float(x)

You can get the text of a BeautifulSoup tag using .get_text()

I got stuck on this one too. IDK if it’s the best way to do it but this is what I did:

webpage = requests.get('https://s3.amazonaws.com/codecademy-content/courses/beautifulsoup/cacao/index.html', 'html.parser')

soup = BeautifulSoup(webpage.content)

soup_rating = soup.select('.Rating')

ratings = []

for i in range(1,len(soup_rating)):
    rating = float(soup_rating[i].get_text())
    ratings.append(rating)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.