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!