Beautiful Soup 提取标题内容

Beautiful Soup extracting title content

使用BeautifulSoup如何获取标题的内容。 假设我试图获得下面的 "I am a title":

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

我看不出哪里出错了,我一直收到这个错误:

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

当 运行:

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

这是一个工作示例:

>>> 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'