は、私はいくつかのxmlを持っている:beautifulsoupのfindAll
<article>
<uselesstag></uslesstag>
<topic>oil, gas</topic>
<body>body text</body>
</article>
<article>
<uselesstag></uslesstag>
<topic>food</topic>
<body>body text</body>
</article>
<article>
<uselesstag></uslesstag>
<topic>cars</topic>
<body>body text</body>
</article>
多く、多くの無駄なタグがあります。 beautifulsoupを使用してbodyタグのテキストをすべて収集し、関連するトピックテキストを使用して新しいxmlを作成したいとします。
私のpythonに新しいですが、私は
import arff
from xml.etree import ElementTree
import re
from StringIO import StringIO
import BeautifulSoup
from BeautifulSoup import BeautifulSoup
totstring=""
with open('reut2-000.sgm', 'r') as inF:
for line in inF:
string=re.sub("[^0-9a-zA-Z<>/\s=!-\"\"]+","", line)
totstring+=string
soup = BeautifulSoup(totstring)
body = soup.find("body")
for anchor in soup.findAll('body'):
#Stick body and its topics in an associated array?
file.close
のいくつかのフォームが動作すると思われます。
1)どうすればいいですか? 2)ルートノードをXMLに追加する必要がありますか?そうでなければ、それは適切なXMLではありませんか?
どうもありがとうございました
編集:私はで終わるしたい何
は次のとおりです。
<article>
<topic>oil, gas</topic>
<body>body text</body>
</article>
<article>
<topic>food</topic>
<body>body text</body>
</article>
<article>
<topic>cars</topic>
<body>body text</body>
</article>
多く、多くの無駄なタグがあります。
ので、タグのA、B、Cからコンテンツを取得したり、すべてのタグのコンテンツを取得し、タグD、E、Fを無視したいですか? –
はい私は2種類のタグ(ボディとトピック)と他のもの(日付、時間など)を無視したい –