![](/img/trans.png)
[英]How to extract SRC attribute from IMG tag with BeautifulSoup?
[英]How to extract attribute value from a tag in BeautifulSoup
我正在尝试从标签(在本例中为TD
)中提取属性的值。 代码如下(HTML文档加载正确; self.data
包含带有HTML数据的字符串,该方法是类的一部分):
def getLine (self):
dat = BeautifulSoup(self.data, "html.parser")
tags = dat.find_all("tr")
for current in tags:
line = current.findChildren("td", recursive=False)
for currentLine in line:
# print (currentLine)
clase = currentLine["class"] # <-- PROBLEMATIC LINE
if clase is not None and "result" in clase:
valor = Line()
valor.name = line.text
错误出现在clase = currentLine["class"]
行中。 我只需要检查标签元素是否具有此属性并执行操作以防它具有值"result"
。
File "C:\DataProgb\urlwrapper.py", line 43, in getLine
clase = currentLine["class"] #Trying to extract attribute class
\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4\element.py", line 1519, in __getitem__
return self.attrs[key]
KeyError: 'class'
它应该可以工作,因为它只是一个元素。 我不明白这个错误。 谢谢。
主要问题是您尝试直接访问属性键,如果属性不可用,将返回KeyError
:
currentLine["class"]
而是使用get()
,它实际上将返回缺少的属性None
:
currentLine.get("class")
从文档 - get(key\[, default\])
:
如果键在字典中,则返回键的值,否则返回默认值。 如果未给出默认值,则默认为无,因此此方法永远不会引发 KeyError。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.