0

抽出する方法はありますか

アルバート アインシュタイン (/ˈælbərt ˈaɪnstaɪn/; ドイツ語: ˈalbɐt ˈaɪnʃtaɪn; 1879 年 3 月 14 日 – 1955 年 4 月 18 日) はドイツ生まれの理論物理学者で、一般相対性理論を開発し、物理学に革命をもたらしました。............. 150 を超える非科学的な作品があります。[6][8] 彼の優れた知性と独創性により、「アインシュタイン」という言葉は天才の代名詞となっています。[9]

(メイン段落の出力全体。コードが実行された場合に表示されます)

次のコードの出力から自動的に? 別のウィキペディアのページから出力された場合でも:

import urllib2
import re, sys
from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def stripHTMLTags(html):
    html = re.sub(r'<{1}br{1}>', '\n', html)
    s = MLStripper()
    s.feed(html)
    text = s.get_data()
    if "External links" in text:
        text, sep, tail = text.partition('External links')
    if "External Links" in text:
        text, sep, tail = text.partition('External Links')
    text = text = text.replace("See also","\n\n See Also - \n")
    text = text.replace("*","- ")
    text = text.replace(".", ". ")
    text = text.replace("  "," ")
    text = text.replace("""   /
 / ""","")
    return text

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
page = infile.read()
print stripHTMLTags(page)

私の書式設定、コード (およびおそらくインデント) の悪さを許してください。

また、投稿がこれを機能させるのに役立った人々にも感謝します:)

4

3 に答える 3

3

どのサイトでも html スクレイピングを行わないことを強くお勧めします。

それを行うのは面倒で、簡単に壊れてしまい、多くのサイト所有者はそれを好まない.

これ ( python-wikitools ) を使用して、ウィキペディア API とやり取りします (長期的には最良の選択です)。

于 2012-04-26T16:15:54.880 に答える
1

次のAPIリクエストは、プレーンテキストのページ抽出を返します: https ://en.wikipedia.org/w/api.php?action = query&prop = extract&titles = Albert%20Einstein&explaintext

于 2012-05-07T20:36:13.067 に答える
-1

OPが直接求めたものであるため、ここに私の答えを残します。これを行う適切な方法は、以下の @ChristophD による回答python-wikitoolsで提案されているように使用することです。


BeautifulSoupを使用するように、質問のコードを少し変更しました。他のオプションがあります。lxmlも試してみてください。

import urllib2
import re, sys
from HTMLParser import HTMLParser

# EDIT 1: import the packag
from BeautifulSoup import BeautifulSoup

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def stripHTMLTags(html):
    html = re.sub(r'<{1}br{1}>', '\n', html)
    s = MLStripper()
    s.feed(html)
    text = s.get_data()
    if "External links" in text:
        text, sep, tail = text.partition('External links')
    if "External Links" in text:
        text, sep, tail = text.partition('External Links')
    text = text = text.replace("See also","\n\n See Also - \n")
    text = text.replace("*","- ")
    text = text.replace(".", ". ")
    text = text.replace("  "," ")
    text = text.replace("""   /
 / ""","")
    return text

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
page = infile.read()

# EDIT 2: convert the page and extract text from the first <p> tag
soup = BeautifulSoup(page)
para = soup.findAll("p", limit=1)[0].text

print stripHTMLTags(para)
于 2012-04-26T16:02:38.470 に答える