Python Forum

Full Version: Scraping data from a web page where same class name applied multiple times
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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")])