Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I extract the value of this HTML element attribute with Beautiful Soup?

I am developing a small tool to scrape a webpage. I am using Beautiful Soup. I would like to fetch the class id from the page. The HTML code looks something like this:

<span class='class_id' id='New_line'></span>

How would I obtain class_id?

like image 871
Kiran Avatar asked Feb 23 '23 08:02

Kiran


2 Answers

This answer refers to an older version of the question where beautifulsoup has not been mentioned

You can use LXML and iterate over all elements asking them for the value of their "class" attribute. LXML is a library for parsing XML documents.

Like, for example:

from lxml import etree
root = etree.parse(filename).getroot()

for span in root.iterdescendants("span"):
    cls = span.attrib.get("class")
like image 50
wal-o-mat Avatar answered Feb 24 '23 23:02

wal-o-mat


Does the following example may help you?

>>> from BeautifulSoup import BeautifulSoup as B
>>> s = B("<span class='class_id' id='New_line'></span>")
>>> s.span.attrs
[(u'class', u'class_id'), (u'id', u'New_line')]
like image 33
luc Avatar answered Feb 24 '23 21:02

luc