Beautiful Soup - Chocolate Scraping Step 13 help

Hi all,

I am having trouble with the Chocolate Scraping Step 13. When I use the code suggested in the hint, I receive a Traceback.

Code is here:- https://gist.github.com/5d44adb17340d951325982078547f3c9

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

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

soup = BeautifulSoup(webpage.content, "html.parser")

all_ratings_tags = soup.find_all(attrs={"class": "Rating"})
ratings=[]
for tag in all_ratings_tags[1:]:
  ratings.append(float(tag.get_text()))
  
plt.hist(ratings)

company_tags = soup.select(".Company")
companies = []

for company in company_tags[1:]:
  companies.append(company.get_text)

df_thingy = {"Company": companies, "Rating": ratings}
dataframe = pd.DataFrame.from_dict(df_thingy)

average_rating = dataframe.groupby("Company").Rating.mean()

It appears I am confusing the Pandas library at Line 28, but I can’t grasp what i am doing wrong! The start of the Traceback is here:-

Traceback (most recent call last):
  File "script.py", line 28, in <module>
    average_rating = dataframe.groupby("Company").Rating.mean()

Hello, @mattsr, and welcome to the Codecademy Forums!

Link to project: Chocolate Scraping with Beautiful Soup

One problem is that the hint uses the name Rating, when it should specify Ratings.

This code corrects that and another problem with the hint:

mean_vals = df.groupby("Company").Ratings.mean()
ten_best = mean_vals.nlargest(10)
print(type(ten_best))

Edited on February 16, 2020 to refine the description of the problems with the hint

While it corrects the hint - my code still won’t run even with the new variable names. The same error is thrown.

In your GitHub code, you have:

for company in company_tags[1:]:
  companies.append(company.get_text)

That raises an error, because you are referencing the company.get_text method without calling it. Parentheses are needed to make the method call.

Edited on February 21, 2020 to add the following:

So that we can assist you most effectively, please supply us with a complete copy of your most recent code. Then we can copy it, attempt to execute it, and locate the errors methodically.

I think it’s all working as expected now. Thanks for the help! Such a small mistake as well…

My code is here in case anyone is interested:-

1 Like