How can i get attribute number

Viewed 64

I use BS4 to parser .xml,i want to get resattribute number,but get none how to do it ?

source xml

 `<digitizer id="1" integrated="true" csrmusttouch="falsehardprox="true" 
    physidcsrs="false"  pnpid="49154" kind="MULTI_TOUCH" maxcsrs="10">      
   <monitor left="0" top="0" right="1920" bottom="1080" />`     
     <properties>          
    <property name="x" logmin="0" logmax="16383" res="621.7457275" unit="cm" hidusage="0x00010030" guid="{598A6A8F-52C0-4BA0-93AF-AF357411A561}" />

    <property name="y" logmin="0" logmax="16383" res="983.9639893" unit="cm" hidusage="0x00010031" guid="{B53F9F75-04E0-4498-A7EE-C30DBB5A9011}" />

    <property name="status" logmin="0" logmax="15" res="0" unit="DEFAULT" hidusage="0x000d0042, 0x000d003c, 0x000d0044" guid="{6E0E07BF-AFE7-4CF7-87D1-AF6446208418}" />

    <property name="time" logmin="0" logmax="2147483647" res="1" unit="DEFAULT" guid="{436510C5-FED3-45D1-8B76-71D3EA7A829D}" />

    <property name="contactid" logmin="0" logmax="31" res="1.861861944" unit="cm" hidusage="0x000d0051" guid="{02585B91-049B-4750-9615-DF8948AB3C9C}" />`

Python Code

a = data_xml.find('digitizer',id="1")
b = a.find('properties')
print(b.get('res'))

Result

None

2 Answers

I have taken your data as html

html="""<digitizer id="1" integrated="true" csrmusttouch="falsehardprox="true" 
    physidcsrs="false"  pnpid="49154" kind="MULTI_TOUCH" maxcsrs="10">      
   <monitor left="0" top="0" right="1920" bottom="1080" />`     
     <properties>          
    <property name="x" logmin="0" logmax="16383" res="621.7457275" unit="cm" hidusage="0x00010030" guid="{598A6A8F-52C0-4BA0-93AF-AF357411A561}" />

    <property name="y" logmin="0" logmax="16383" res="983.9639893" unit="cm" hidusage="0x00010031" guid="{B53F9F75-04E0-4498-A7EE-C30DBB5A9011}" />

    <property name="status" logmin="0" logmax="15" res="0" unit="DEFAULT" hidusage="0x000d0042, 0x000d003c, 0x000d0044" guid="{6E0E07BF-AFE7-4CF7-87D1-AF6446208418}" />

    <property name="time" logmin="0" logmax="2147483647" res="1" unit="DEFAULT" guid="{436510C5-FED3-45D1-8B76-71D3EA7A829D}" />

    <property name="contactid" logmin="0" logmax="31" res="1.861861944" unit="cm" hidusage="0x000d0051" guid="{02585B91-049B-4750-9615-DF8948AB3C9C}" />"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"html.parser")

Code::

You can find all property tag and then find res value associate to it!

a = soup.find('digitizer',attrs={"id":"1"})
properties=a.find_all("property")
res_lst=[i['res'] for i in properties]

Output::

['621.7457275', '983.9639893', '0', '1', '1.861861944']

Your xml seems poorly formatted, after reformatting it:

<digitizer id="1" integrated="true" csrmusttouch="" falsehardprox="true" physidcsrs="false"  pnpid="49154" kind="MULTI_TOUCH" maxcsrs="10">
    <monitor left="0" top="0" right="1920" bottom="1080"/>
    <properties>
        <property name="x" logmin="0" logmax="16383" res="621.7457275" unit="cm" hidusage="0x00010030" guid="{598A6A8F-52C0-4BA0-93AF-AF357411A561}" />
        <property name="y" logmin="0" logmax="16383" res="983.9639893" unit="cm" hidusage="0x00010031" guid="{B53F9F75-04E0-4498-A7EE-C30DBB5A9011}" />
        <property name="status" logmin="0" logmax="15" res="0" unit="DEFAULT" hidusage="0x000d0042, 0x000d003c, 0x000d0044" guid="{6E0E07BF-AFE7-4CF7-87D1-AF6446208418}" />
        <property name="time" logmin="0" logmax="2147483647" res="1" unit="DEFAULT" guid="{436510C5-FED3-45D1-8B76-71D3EA7A829D}" />
        <property name="contactid" logmin="0" logmax="31" res="1.861861944" unit="cm" hidusage="0x000d0051" guid="{02585B91-049B-4750-9615-DF8948AB3C9C}" />

You can easily parse it like this:

from bs4 import BeautifulSoup

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

for element in results.find_all("properties"):
    for property_tag in element.find_all("property"):
        print(property_tag['res'])

Output:

621.7457275
983.9639893
0
1
1.861861944

You can find more info about parsing attribute values from xml in the tutorial where the code is from.

Edit: Note that I slightly modified the code to fit your question.

Related