私はいくつかの特定のフレーズについて約500のXMLドキュメントを検索し、それらのフレーズのいずれかを含む要素のIDを出力しようとしています。現在、これは私のコードです:適切にこの意志の出力のようなlxmlを使用すると、ネストされた要素内のテキストをどのように読み取ることができますか?
from lxml import etree
import os
import re
files = os.listdir('C:/Users/Me/Desktop/xml')
search_words = ['House divided', 'Committee divided', 'on Division', 'Division List',
'The Ayes and the Noes',]
for f in files:
doc = etree.parse('C:/Users/Me/Desktop/xml/' +f)
for elem in doc.iter():
for word in search_words:
if elem.text is not None and str(elem.attrib) != "{}" and word in elem.text and len(re.findall(r'\d+', elem.text))>1:
votes = re.findall(r'\d+', elem.text)
string = str(elem.attrib)[8:-2] + ","
string += (str(votes[0]) + "," + str(votes[1]) + ",")
string += word + ","
string += str(elem.sourceline)
print string
入力:テキストは内部のフレーズを解析されていませんので、このようなネストされた要素を持つ
<p id="S3V0001P0-01869">The House divided; Against the Motion 83; For it 23—Majority 60.</p>
しかし、入力は、見逃すことになります:
<p id="S3V0141P0-01248"><member>THE CHANCELLOR OF THE EXCHEQUER</member><membercontribution> said, that the precedent occurred on the 8th of April, 1850, on a Motion for going into a Committee of Supply. An Amendment was moved by Captain Boldero on the subject of assistant-surgeons in the navy, when, on a division being called for, the Question was put that the words proposed to be left out stand part of the Question. The House divided, when the numbers were—Ayes, 40; Noes, 48. The Question, "That the proposed words be added" was put and agreed to; the main Question, as amended, was put and agreed to; and the Question being then put, "That Mr. Speaker do now leave the chair," that Motion was agreed to, and the House went into Committee of Supply.</membercontribution></p>
は、このようなネストされた要素内のテキストを読み、そのIDを返す方法はありますか?