问候,我想在python中解析val =""从服务器返回的XML字符串
任何帮助都是apreciated
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type='text/xsl' href='/obix/xsl'?>
<real xmlns:c2g="http://www.can2go.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" writable="true" href="http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/" xsi:schemaLocation="http://obix.org/ns/schema/1.0/obix/xsd" xmlns="http://obix.org/ns/schema/1.0" val="71" />
这是我的代码atm。
#!/usr/bin/python
import requests
value = 71
payload = "<real val=\""+str(value)+"\" />"
url = "http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/"
def getObix():
r = requests.get(url)
print r.text
def putObix():
r = requests.post(url, payload)
getObix()
#putObix()
答案 0 :(得分:1)
你可以使用BeautifulSoup。
>>> from bs4 import BeautifulSoup
>>> s = '''<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type='text/xsl' href='/obix/xsl'?>
<real xmlns:c2g="http://www.can2go.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" writable="true" href="http://192.168.1.133/obix/network/N002061/DEV100/AV1/Present_Value/val=/" xsi:schemaLocation="http://obix.org/ns/schema/1.0/obix/xsd" xmlns="http://obix.org/ns/schema/1.0" val="71" />'''
>>> soup = BeautifulSoup(s)
>>> [i['val'] for i in soup.find_all('real')]
['71']
答案 1 :(得分:1)
如果有人有兴趣我发现了
#!/usr/bin/python
import requests
from xml.etree import ElementTree
url = "http://192.168.1.133/obix/network/N002061/DEV100/AV1/"
def getObix():
r = requests.get(url)
xml = ElementTree.fromstring(r.content)
print "AV1 Descriptor = "+xml[2].attrib["val"]
print "AV1 Present_Value = "+xml[5].attrib["val"]
print "AV1 Display = "+xml[6].attrib["display"]
print "AV1 Units = "+xml[7].attrib["val"]