5

私はいくつかのxmlを持っています:

<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>

意味のないタグがたくさんあります。

4

2 に答える 2

9

わかった。これが解決策です、

まず、uに「beautifulsoup4」がインストールされていることを確認します:http ://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-beautiful-soup

これが、すべての本文タグとトピックタグを取得するための私のコードです。

from bs4 import BeautifulSoup
html_doc= """
<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>
"""
soup = BeautifulSoup(html_doc)

bodies = [a.get_text() for a in soup.find_all('body')]
topics = [a.get_text() for a in soup.find_all('topic')]
于 2012-05-09T15:49:14.227 に答える
1

空の xml または html タグを削除する別の方法は、再帰関数を使用して空のタグを検索し、.extract() を使用してそれらを削除することです。このように、保持したいタグを手動でリストする必要はありません。また、ネストされた空のタグのクリーニングも可能にします。

from bs4 import BeautifulSoup
import re
nonwhite=re.compile(r'\S+',re.U)

html_doc1="""
<article>
<uselesstag2>
<uselesstag1>
</uselesstag1>
</uselesstag2>
<topic>oil, gas</topic>
<body>body text</body>
</article>

<p>21.09.2009</p> 
<p> </p> 
<p1><img src="http://www.www.com/"></p1> 
<p></p> 

<!--- This article is about cars--->
<article>
<topic>cars</topic>
<body>body text</body>
</article>
"""

def nothing_inside(thing):
    # select only tags to examine, leave comments/strings
    try:
        # check for img empty tags
        if thing.name=='img' and thing['src']<>'':
            return False
        else:
            pass
        # check if any non-whitespace contents
        for item in thing.contents:
            if nonwhite.match(item):
                return False
            else:
                pass
        return True
    except:
        return False

def scrub(thing):
    # loop function as long as an empty tag exists
    while thing.find_all(nothing_inside,recursive=True) <> []:
        for emptytag in thing.find_all(nothing_inside,recursive=True):
            emptytag.extract()
            scrub(thing)
    return thing

soup=BeautifulSoup(html_doc1)
print scrub(soup)

結果:

<article>

<topic>oil, gas</topic>
<body>body text</body>
</article>
<p>21.09.2009</p>

<p1><img src="http://www.www.com/"/></p1>

<!--- This article is about cars--->
<article>
<topic>cars</topic>
<body>body text</body>
</article>
于 2012-08-16T16:51:45.373 に答える