我正在尝试提取网页上特定"input“标签中的单个"value”属性的内容。我使用以下代码:
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:列表索引必须是整数,而不是字符串
尽管我从Beautifulsoup文档中了解到字符串在这里应该不是问题……但我不是专家,我可能误解了。
任何建议都是非常感谢的!
发布于 2010-04-10 15:06:28
.find_all()返回所有找到的元素的列表,因此:
input_tag = soup.find_all(attrs={"name" : "stainfo"})input_tag是一个列表(可能只包含一个元素)。根据你到底想要什么,你要么应该这样做:
output = input_tag[0]['value']或者使用.find()方法,它只返回一个(第一个)找到的元素:
input_tag = soup.find(attrs={"name": "stainfo"})
output = input_tag['value']发布于 2016-11-17 03:36:41
在Python 3.x中,只需对使用find_all获得的tag对象使用get(attr_name)
xmlData = None
with open('conf//test1.xml', 'r') as xmlFile:
xmlData = xmlFile.read()
xmlDecoded = xmlData
xmlSoup = BeautifulSoup(xmlData, 'html.parser')
repElemList = xmlSoup.find_all('repeatingelement')
for repElem in repElemList:
print("Processing repElem...")
repElemID = repElem.get('id')
repElemName = repElem.get('name')
print("Attribute id = %s" % repElemID)
print("Attribute name = %s" % repElemName)对XML文件conf//test1.xml,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<singleElement>
<subElementX>XYZ</subElementX>
</singleElement>
<repeatingElement id="11" name="Joe"/>
<repeatingElement id="12" name="Mary"/>
</root>打印:
Processing repElem...
Attribute id = 11
Attribute name = Joe
Processing repElem...
Attribute id = 12
Attribute name = Mary发布于 2012-08-28 23:35:56
如果您想从上面的源中检索多个属性值,您可以使用findAll和列表理解来获取所需的一切:
import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)
inputTags = soup.findAll(attrs={"name" : "stainfo"})
### You may be able to do findAll("input", attrs={"name" : "stainfo"})
output = [x["stainfo"] for x in inputTags]
print output
### This will print a list of the values.https://stackoverflow.com/questions/2612548
复制相似问题