首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用漂亮汤提取属性值

使用漂亮汤提取属性值
EN

Stack Overflow用户
提问于 2010-04-10 14:53:01
回答 10查看 295.8K关注 0票数 170

我正在尝试提取网页上特定"input“标签中的单个"value”属性的内容。我使用以下代码:

代码语言:javascript
运行
复制
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文档中了解到字符串在这里应该不是问题……但我不是专家,我可能误解了。

任何建议都是非常感谢的!

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2010-04-10 15:06:28

.find_all()返回所有找到的元素的列表,因此:

代码语言:javascript
运行
复制
input_tag = soup.find_all(attrs={"name" : "stainfo"})

input_tag是一个列表(可能只包含一个元素)。根据你到底想要什么,你要么应该这样做:

代码语言:javascript
运行
复制
output = input_tag[0]['value']

或者使用.find()方法,它只返回一个(第一个)找到的元素:

代码语言:javascript
运行
复制
input_tag = soup.find(attrs={"name": "stainfo"})
output = input_tag['value']
票数 210
EN

Stack Overflow用户

发布于 2016-11-17 03:36:41

Python 3.x中,只需对使用find_all获得的tag对象使用get(attr_name)

代码语言:javascript
运行
复制
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,如下所示:

代码语言:javascript
运行
复制
<?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>

打印:

代码语言:javascript
运行
复制
Processing repElem...
Attribute id = 11
Attribute name = Joe
Processing repElem...
Attribute id = 12
Attribute name = Mary
票数 40
EN

Stack Overflow用户

发布于 2012-08-28 23:35:56

如果您想从上面的源中检索多个属性值,您可以使用findAll和列表理解来获取所需的一切:

代码语言:javascript
运行
复制
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.
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2612548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档