pythonhtmlweb-scrapingbeautifulsoup

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?


Solution

  • 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')]