如何使用BeautifulSoup和Python获取属性值? [英] How to get an attribute value using BeautifulSoup and Python?

查看:68
本文介绍了如何使用BeautifulSoup和Python获取属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用BeautifulSoup和Python获取属性值. XML的结构如下:

I'm failing miserably to get an attribute value using BeautifulSoup and Python. Here is how the XML is structured:

...
</total>
<tag>
    <stat fail="0" pass="1">TR=111111 Sandbox=3000613</stat>
    <stat fail="0" pass="1">TR=121212 Sandbox=3000618</stat>
    ...
    <stat fail="0" pass="1">TR=999999 Sandbox=3000617</stat>
</tag>
<suite>
...

我要获取的是pass值,但是对于我一生,我只是不知道该怎么做.我检查了 BeautifulSoup ,看来我应该使用类似stat['pass'],但这似乎不起作用.

What I'm trying to get is the pass value, but for the life of me I just can't understand how to do it. I checked the BeautifulSoup and it seems that I should be using something like stat['pass'], but that doesn't seem to work.

这是我的代码:

with open('../results/output.xml') as raw_resuls:
results = soup(raw_resuls, 'lxml')
for stat in results.find_all('tag'):
            print stat['pass']

如果执行results.stat['pass'],它将返回另一个标记内的值,该值在XML blob中向上.

If I do results.stat['pass'] it returns a value that is within another tag, way up in the XML blob.

如果我打印stat变量,则会得到以下信息:

If I print the stat variable I get the following:

<stat fail="0" pass="1">TR=787878 Sandbox=3000614</stat>
...
<stat fail="0" pass="1">TR=888888 Sandbox=3000610</stat>

似乎还可以.

我很确定我缺少某些东西或做错了什么.我应该在哪里看?我采用了错误的方法吗?

I'm pretty sure that I'm missing something or doing something wrong. Where should I be looking at? Am I taking the wrong approach?

任何建议或指导将不胜感激!谢谢

Any advice or guidance will be greatly appreciated! Thanks

推荐答案

请考虑以下方法:

from bs4 import BeautifulSoup

with open('test.xml') as raw_resuls:
    results = BeautifulSoup(raw_resuls, 'lxml')

for element in results.find_all("tag"):
    for stat in element.find_all("stat"):
        print(stat['pass'])

您的解决方案的问题是 pass 包含在 stat 中,而不包含在您搜索它的 tag 中.

The problem of your solution is that pass is contained in stat and not in tag where you search for it.

此解决方案搜索所有 tag ,并在这些 tag 中搜索 stat .从这些结果中可以得到 pass .

This solution searches for all tag and in these tag it searches for stat. From these results it gets pass.

对于XML文件

<tag>
    <stat fail="0" pass="1">TR=111111 Sandbox=3000613</stat>
    <stat fail="0" pass="1">TR=121212 Sandbox=3000618</stat>
    <stat fail="0" pass="1">TR=999999 Sandbox=3000617</stat>
</tag>

上面的脚本获取输出

1
1
1

添加

由于某些细节似乎仍然不清楚(请参阅注释),请考虑使用BeautifulSoup以获得所需的一切的完整解决方法.如果您遇到性能问题,则使用字典作为列表元素的解决方案可能并不完美.但是,由于您似乎在使用Python和Soup时遇到了一些麻烦,因此我认为我可以通过按名称而不是按索引访问所有相关信息的方式来尽可能地简化此示例.

Since some detailes still seemed to be unclear (see comments) consider this complete workaround using BeautifulSoup to get everything you want. This solution using dictionaries as elements of lists might not be perfect if you face performance issues. But since you seem to have some troubles using the Python and Soup i thought I create this example as easy as possible by giving the possibility to access all relevant information by name and not by an index.

from bs4 import BeautifulSoup

# Parses a string of form 'TR=abc123 Sandbox=abc123' and stores it in a dictionary with the following
# structure: {'TR': abc123, 'Sandbox': abc123}. Returns this dictionary. 
def parseTestID(testid):
    dict = {'TR': testid.split(" ")[0].split("=")[1], 'Sandbox': testid.split(" ")[1].split("=")[1]}
    return dict

# Parses the XML content of 'rawdata' and stores pass value, TR-ID and Sandbox-ID in a dictionary of the 
# following form: {'Pass': pasvalue, TR': TR-ID, 'Sandbox': Sandbox-ID}. This dictionary is appended to
# a list that is returned.
def getTestState(rawdata):
    # initialize parser
    soup = BeautifulSoup(rawdata,'lxml')
    parsedData= []

    # parse for tags
    for tag in soup.find_all("tag"):
        # parse tags for stat
        for stat in tag.find_all("stat"):
            # store everthing in a dictionary
            dict = {'Pass': stat['pass'], 'TR': parseTestID(stat.string)['TR'], 'Sandbox': parseTestID(stat.string)['Sandbox']}
            # append dictionary to list
            parsedData.append(dict)

    # return list
    return parsedData

您可以按以下方式使用上面的脚本来执行所需的任何操作(例如,只需打印出来)

You can use the script above as follows to do whatever you want (e.g. just print out)

# open file
with open('test.xml') as raw_resuls:
    # get list of parsed data 
    data = getTestState(raw_resuls)

# print parsed data
for element in data:
    print("TR = {0}\tSandbox = {1}\tPass = {2}".format(element['TR'],element['Sandbox'],element['Pass']))

输出看起来像这样

TR = 111111 Sandbox = 3000613   Pass = 1
TR = 121212 Sandbox = 3000618   Pass = 1
TR = 222222 Sandbox = 3000612   Pass = 1
TR = 232323 Sandbox = 3000618   Pass = 1
TR = 333333 Sandbox = 3000605   Pass = 1
TR = 343434 Sandbox = ZZZZZZ    Pass = 1
TR = 444444 Sandbox = 3000604   Pass = 1
TR = 454545 Sandbox = 3000608   Pass = 1
TR = 545454 Sandbox = XXXXXX    Pass = 1
TR = 555555 Sandbox = 3000617   Pass = 1
TR = 565656 Sandbox = 3000615   Pass = 1
TR = 626262 Sandbox = 3000602   Pass = 1
TR = 666666 Sandbox = 3000616   Pass = 1
TR = 676767 Sandbox = 3000599   Pass = 1
TR = 737373 Sandbox = 3000603   Pass = 1
TR = 777777 Sandbox = 3000611   Pass = 1
TR = 787878 Sandbox = 3000614   Pass = 1
TR = 828282 Sandbox = 3000600   Pass = 1
TR = 888888 Sandbox = 3000610   Pass = 1
TR = 999999 Sandbox = 3000617   Pass = 1

让我们总结一下所使用的核心元素:

Let's summerize the core elements that are used:

查找XML标签 要查找XML标签,请使用soup.find("tag")返回第一个匹配的标签,或者使用soup.find_all("tag")查找所有匹配的标签并将它们存储在列表中.通过遍历列表,可以轻松访问单个标签.

Finding XML tags To find XML tags you use soup.find("tag") which returns the first matched tag or soup.find_all("tag") which finds all matching tags and stores them in a list. The single tags can easily be accessed by iterating over the list.

查找嵌套标签 要查找嵌套标签,可以将find()find_all()应用于第一个find_all()的结果,再次使用它.

Finding nested tags To find nested tags you can use find() or find_all() again by applying it to the result of the first find_all().

访问标签的内容 要访问标签的内容,可将string应用于单个标签.例如,如果tag = <tag>I love Soup!</tag> tag.string = "I love Soup!".

Accessing the content of a tag To access the content of a tag you apply string to a single tag. For example if tag = <tag>I love Soup!</tag> tag.string = "I love Soup!".

查找属性值 要获取属性的值,可以使用下标表示法.例如,如果tag = <tag color=red>I love Soup!</tag> tag['color']="red".

Finding values of attributes To get the values of attributes you can use the subscript notation. For example if tag = <tag color=red>I love Soup!</tag> tag['color']="red".

对于解析形式为"TR=abc123 Sandbox=abc123"的字符串,我使用了常见的Python字符串拆分方法.您可以在此处了解更多信息:如何拆分并在Python中解析字符串?

For parsing strings of form "TR=abc123 Sandbox=abc123" I used common Python string splitting. You can read more about it here: How can I split and parse a string in Python?

这篇关于如何使用BeautifulSoup和Python获取属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆