我试图提取网页上特定“输入”标记中单个“值”属性的内容。我使用以下代码:
import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)
inputTag = soup.findAll(attrs={"name" : "stainfo"})
output = inputTag['value']
print str(output)
我得到一个TypeError:列表索引必须是整数,而不是str
尽管从Beautifulsoup文档中我了解到字符串在这里不应该是一个问题。。。但我不是专家,我可能误解了。
任何建议都非常感谢! 提前谢谢。
如果要从上面的源检索属性的多个值,可以使用
findAll
和列表理解来获取所需的所有内容:.findAll()
返回所有找到的元素的列表,因此:inputTag
是一个列表(可能只包含一个元素)。根据您的具体要求,您应该:或者使用只返回一个(第一个)找到的元素的
.find()
方法:在
Python 3.x
中,只需在使用find_all
获得的标记对象上使用get(attr_name)
:对照XML文件
conf//test1.xml
,它看起来像:印刷品:
相关问题 更多 >
编程相关推荐