如何使用python和beautifulsoup获取标题属性? [英] How to obtain title attribute using python and beautifulsoup?
问题描述
假设以下内容:
<td title="I want this title" role="gridcell"><a onclick="open" href="#">TEXT</a></td>
现在,我已经成功使用以下方法分别找到了表和单个行:
Now, I've successfully found respectively the table and individual rows using:
for rows in soup.find_all(['tr']):
for cells in rows.find_all(['td']):
通过打印cells
我可以看到我找到了正确的行,但是我真的不确定如何获取title属性并将其另存为字符串吗?我尝试使用temp = soup.find('td')['title']
,但是这样做会出错,所以很明显我做错了.
By printing cells
I can see I've found the correct rows, but I'm really not sure how to take the title attribute and save it as a string? I've attempted to use temp = soup.find('td')['title']
, but I'm getting errors doing this, so evidently I'm doing something wrong.
任何建议将不胜感激!
推荐答案
To get an attribute of an element, you can treat an element as a dictionary (reference):
soup.find('tag_name')['attribute_name']
而且,在您的情况下:
for tr in soup.find_all('tr'):
for td in tr.find_all('td'):
print(td.get('title', 'No title attribute'))
请注意,我已使用.get()
方法来避免在没有title
属性的td
元素上失败.
Note that I've used .get()
method to avoid failing on td
elements with no title
attribute.
这篇关于如何使用python和beautifulsoup获取标题属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!