Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class attribute using Beautiful soup

I am having trouble with class attributes using Beautiful Soup.

My code is :

ls = ([x.get_text().strip('\n') for x in bs.find_all("div",{"class":"other-info","class":"description","class":"n-title search-detail"})])

But I get only description class not other class information. How can I get other class information in same code.

like image 974
sonia Avatar asked Jun 23 '26 07:06

sonia


1 Answers

Firstly, you can use strip=True with the get_text() function to strip all the whitespace from the text. Secondly, a dictionary can't have duplicate keys. The find_all() function accepts a list as a value for an attribute. Also, those brackets () around the list are redundant. Remove them.

Use this:

ls = [x.get_text(strip=True) for x in bs.find_all("div", {"class": ["other-info", "description", "n-title search-detail"]})]
like image 92
Keyur Potdar Avatar answered Jun 25 '26 21:06

Keyur Potdar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!