Search code examples
pythonbeautifulsoupattributeerror

Beautiful Soup extracting title content


Using BeautifulSoup how do you get the content of the title. Say I was trying to get the "I am a title" below:

h4 class="title" title="I am a title"

I can't see where I'm going wrong, I keep getting this error:

AttributeError: 'NoneType' object has no attribute 'attrs'

when running:

product_name = self.parent.select_one(locator).attrs['title']

Solution

  • Here's a working example:

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup('<html><h4 class="title" title="I am a title">test</h4></html>')
    >>> soup.find(attrs={'class': 'title'})['title']
    'I am a title'