如何使用python和beautifulsoup获取title属性? [英] 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获取title属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!