Python Forum
Scraping data from a web page where same class name applied multiple times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scraping data from a web page where same class name applied multiple times
#1
I am retrieving data from a webpage using beautifulsoup. There I observed in the code that same class name is applied multiple times from where I want to retrieve the data.

For example-

print(soup.find(class_="_2lzr _50f5 _50f7").text)
above is the code I am using to retrieve the data. I observed in the html code that there are multiple classes with the name "_2lzr _50f5 _50f7", but this print statement only prints the text of first one.

I also tried this -

print(soup.find_all(class_="_2lzr _50f5 _50f7").text)
but I am getting an error like below -

Error:
Traceback (most recent call last): File "data_fetching.py", line 15, in <module> print(soup.find_all(class_="_2lzr _50f5 _50f7").text) File "/usr/lib/python3.6/site-packages/bs4/element.py", line 1807, in __getattr__ "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
though the soup selects the all with class name "_2lzr _50f5 _50f7" but not able to display the text.

I want to know the solution, how can I retrieve the data either one by one or at time?
Reply
#2
find_all will return list of objects. you need to iterate over it and print it
for my_tag in soup.find_all(class_="_2lzr _50f5 _50f7"):
    print(my_tag.text)
or use list comprehension

print([my_tag.text for my_tag in soup.find_all(class_="_2lzr _50f5 _50f7")])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to scraping data from dinamic site sergio21124444 2 683 Nov-08-2023, 12:43 PM
Last Post: sergio21124444
  I am scraping a web page but got an Error Sarmad54 3 1,452 Mar-02-2023, 08:20 PM
Last Post: Sarmad54
Information Web-scraping, multiple webpages Pabloty92 1 1,274 Dec-28-2022, 02:09 PM
Last Post: Yoriz
  Scraping data from table into existing dataframe vincer58 1 2,008 Jan-09-2022, 05:15 PM
Last Post: vincer58
  Scraping the page without distorting content oleglpts 5 2,486 Dec-16-2021, 05:08 PM
Last Post: oleglpts
  trying to save data automatically from this page thunderspeed 1 2,008 Sep-19-2021, 04:57 AM
Last Post: ndc85430
  Web scraping data Mike_Eddy 2 2,539 Jul-03-2021, 05:49 PM
Last Post: Mike_Eddy
  Scraping lender data from Ren Ren Dai website using Python. I will pay for that 200$ Hafedh_2021 1 2,753 May-18-2021, 08:41 PM
Last Post: snippsat
  Scraping a page with log in data (security, proxies) iamaghost 0 2,144 Mar-27-2021, 02:56 PM
Last Post: iamaghost
  Scraping .aspx page Larz60+ 21 51,152 Mar-18-2021, 10:16 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020