使用 python 和 XML XPath 时获取密钥类型错误
Getting Key type Error in using python and XML XPath
输入 XML 属性时出现类型错误
XML 从中获取数据的文件
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
Python代码的第一部分接受用户的输入并匹配XML中的属性以显示文件
第二部分Python代码到modify/change错误所在的属性
ch = input("\nEnter Tag you want to display : ") #First part
for x in root.findall(".//vulnerability[@type = '{}']".format(ch)):
print(x.tag,"--->",x.attrib)
changes = input("\nEnter Your changed tag : ") #Second Part
for x in root.findall(".//vulnerability"):
if x.attrib['type'] == ch: #ERROR
x.attrib['type'] = changes #Error
print(x.tag,x.attrib)
检查下面的代码:
import xml.etree.ElementTree as ET
xml = """<?xml version="1.0"?>
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
"""
tree = ET.fromstring(xml)
第一种方法.attrib()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.attrib['type'] = f'{changes}'
print(ET.tostring(tree))
第二种方法.get()
和.set()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.get(f'{ch}')
x.set('type', f'{changes}')
print(ET.tostring(tree))
输入 XML 属性时出现类型错误
XML 从中获取数据的文件
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
Python代码的第一部分接受用户的输入并匹配XML中的属性以显示文件
第二部分Python代码到modify/change错误所在的属性
ch = input("\nEnter Tag you want to display : ") #First part
for x in root.findall(".//vulnerability[@type = '{}']".format(ch)):
print(x.tag,"--->",x.attrib)
changes = input("\nEnter Your changed tag : ") #Second Part
for x in root.findall(".//vulnerability"):
if x.attrib['type'] == ch: #ERROR
x.attrib['type'] = changes #Error
print(x.tag,x.attrib)
检查下面的代码:
import xml.etree.ElementTree as ET
xml = """<?xml version="1.0"?>
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
"""
tree = ET.fromstring(xml)
第一种方法.attrib()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.attrib['type'] = f'{changes}'
print(ET.tostring(tree))
第二种方法.get()
和.set()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.get(f'{ch}')
x.set('type', f'{changes}')
print(ET.tostring(tree))