Pull tag value with BeautifulSoup

Can someone guide me how to pull the tag value using BeautifulSoup? I read the documentation, but it was difficult for me to navigate through it. For example, if I had:

<span title="Funstuff" class="thisClass">Fun Text</span> 

How would I just pull out "Funstuff" in busing BeautifulSoup / Python?

Edit: I am using version 3.2.1

+6
source share
2 answers

You need to define something for the element you are looking for, and it is hard to say what exactly is in this matter.

For example, both of them print "Funstuff" in BeautifulSoup 3. One searches for the span element and gets the title, and the other looks for gaps with this class. Many other feasible ways to achieve this goal are possible.

 import BeautifulSoup soup = BeautifulSoup.BeautifulSoup('<html><body><span title="Funstuff" class="thisClass">Fun Text</span></body></html>') print soup.html.body.span['title'] print soup.find('span', {"class": "thisClass"})['title'] 
+6
source

Tags available to children are available through .contents http://www.crummy.com/software/BeautifulSoup/bs4/doc/#contents-and-children In your case, you can find the tag using your CSS class to extract the content

 from bs4 import BeautifulSoup soup=BeautifulSoup('<span title="Funstuff" class="thisClass">Fun Text</span>') soup.select('.thisClass')[0].contents[0] 

http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors contains all nepessary details

+1
source

Source: https://habr.com/ru/post/921149/


All Articles