用与beautifulsoup属性值 [英] Extracting an attribute value with beautifulsoup
问题描述
我试图提取一个值属性的内容在网页上的一个特定的input标记。我用下面的code:
I am trying to extract the content of a single "value" attribute in a specific "input" tag on a webpage. I use the following code:
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)
我得到一个类型错误:列表索引必须是整数,而不是str的
I get a TypeError: list indices must be integers, not str
尽管从Beautifulsoup文档,我的理解是字符串不应该是一个问题在这里...但我没有一个专家,我可能误解了。
even though from the Beautifulsoup documentation i understand that strings should not be a problem here... but i a no specialist and i may have misunderstood.
任何建议大大AP preciated!
先谢谢了。
Any suggestion is greatly appreciated! Thanks in advance.
推荐答案
.findAll()
返回所有找到的元素的列表,因此:
.findAll()
returns list of all found elements, so:
inputTag = soup.findAll(attrs={"name" : "stainfo"})
inputTag
是一个列表(可能只包含一个元素)。这取决于你想要什么,你不是应该做的:
inputTag
is a list (probably containing only one element). Depending on what you want exactly you either should do:
output = inputTag[0]['value']
或使用 .find()
方法,它返回只有一个(第一)找到的元素:
or use .find()
method which returns only one (first) found element:
inputTag = soup.find(attrs={"name": "stainfo"})
output = inputTag['value']
这篇关于用与beautifulsoup属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!