您可以使用BeautifulSoup,这是一个示例:
from bs4 import BeautifulSoup as bs
xml = """<Test_input_data>
<web_pins type="list" orientation="column">
<web_pin_id type="column_definition" data_type="int" index="1"/>
<pin_center_node type="column_definition" data_type="int" index="2"/>
<journal_center_node type="column_definition" data_type="int" index="3"/>
1 1012 5011
2 2012 5012
3 3012 5013
</web_pins>
</Test_input_data>"""
soup = bs(xml)
tag = soup.find("web_pins")
text = tag.text #Here you get your text!
##P.S. you can also use:
soup.web_pins.text
此外,您可能想查看标准的库
HTMLParser。同样,这里有一个例子:
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
data = ""
search_tag = None
grab_data = False
def feed(self, data, tag_to_search_for):
self.search_tag = tag_to_search_for
HTMLParser.feed(self, data)
def handle_starttag(self, tag, attrs):
if tag == self.search_tag:
self.grab_data = 1
def handle_data(self, data):
if self.grab_data:
self.data = data
def handle_endtag(self, tag):
if tag == self.search_tag:
self.grab_data = 0
xml = """<Test_input_data>
<web_pins type="list" orientation="column">
<web_pin_id type="column_definition" data_type="int" index="1"/>
<pin_center_node type="column_definition" data_type="int" index="2"/>
<journal_center_node type="column_definition" data_type="int" index="3"/>
1 1012 5011
2 2012 5012
3 3012 5013
</web_pins>
</Test_input_data>"""
parser = MyHTMLParser()
parser.feed(xml, "web_pins")
print parser.data #Ta-daa!