LoginSignup
ryo06
@ryo06 (Momo Yuuu)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

WikiExtractor.py が実行できません

pythonでWikipediaのダンプファイルのマークアップを取り除いて文章を抜き出そうと思い、

wget https://github.com/attardi/wikiextractor/blob/master/wikiextractor/WikiExtractor.py

でスクリプトをダウンロードしました。
そして、

python WikiExtractor.py --no_templates -o articles -b 100M ファイルパス

で実行したところ下記のような構文エラーが発生しました。

発生している問題・エラー

File "WikiExtractor.py", line 7
    <!DOCTYPE html>
    ^
SyntaxError: invalid syntax

全てのファイルは同じ階層においており、色々調べて試行錯誤しましたがエラーを解消できません。
まだ知識が浅くわかりずらい説明ですが、教えていただけると助かります。。。

0

1Answer

最初の回答

不勉強で、wgetについて存じないのですが、もしかしてWebコンテンツ(HTMLファイルなど)をダウンロードするツールではないでしょうか?
GitHubからソースファイルをダウンロードするのでしたら、ページにある「zipでダウンロード」(下図)や、GitHub Desktopなどのツールを使うのが良いと思います。

zipでダウンロードのリンクの様子

追記

実際に試してみました。

環境
  • Python 3.8.5
  • conda 4.9.2
  • Windows 10 (20H2)

モジュールのダウンロードと展開

前述のリンクからダウンロードしたzipを以下のように展開しました。

wikiextractor-master
│  .gitignore
│  extract.sh
│  jawiki-latest-abstract.xml
│  LICENSE
│  README.md
│  setup.py
│  tree.txt
│  
├─.github
│  └─workflows
│          python-publish.yml
│          
├─text
│  └─AA
│          wiki_00
│          
└─wikiextractor
    │  cirrus-extract.py
    │  clean.py
    │  extract.py
    │  extractPage.py
    │  WikiExtractor.py
    │  __init__.py
    │  
    └─__pycache__
            extract.cpython-38.pyc
            WikiExtractor.cpython-38.pyc
            __init__.cpython-38.pyc

ダンプファイルの取得

よく分からないままに、「ウィキペディア日本語版のダンプ」から適当にファイルを1個だけダウンロードして展開しました。

ダウンロードしたファイル: jawiki-latest-abstract.xml.gz
展開して得たファイル: jawiki-latest-abstract.xml

実行

Anaconda Promptを開き、カレントディレクトリを前述の展開先フォルダwikiextractor-masterにして、README.mdの「Usage」に「The script is invoked with a Wikipedia dump file as an argument:」と書かれている通りに実行してみました。

wikiextractor-master>python -m wikiextractor.WikiExtractor jawiki-latest-abstract.xml

すると、以下のような出力が始まりました。

INFO: Preprocessing 'jawiki-latest-abstract.xml' to collect template definitions: this may take some time.
INFO: Loaded 0 templates in 0.0s
INFO: Starting page extraction from jawiki-latest-abstract.xml.
~

読む限り、少なくとも、抽出処理は開始できているようです。

0

Comments

  1. @ryo06

    Questioner
    お答え頂きありがとうございます!
    試させて頂きましたがやはりダメでした。。。
  2. 何をお試しになられ、どのような結果になったのでしょうか。
    よろしければ、以下を順にお試しいただいて、それぞれどのような結果になったか教えていただけないでしょうか。

    (1) 以下のリンク(先述の「zipでダウンロード」)からダウンロードしてください。
    https://github.com/attardi/wikiextractor/archive/master.zip

    (2) ダウンロードしたzipファイルを展開してください。

    (3) 展開したフォルダの中の'WikiExtractor​.​py'を実行してください。
  3. @ryo06

    Questioner
    ご丁寧にありがとうございます!

    以下のように実行した所エラーになります。

    1. https://github.com/attardi/wikiextractor/archive/master.zip のダウンロード。

    2. python WikiExtractor.py --no_templates -o articles -b 100M ファイルパス  で実行。

    --エラー内容--
    python: can't open file 'WikiExtractor.py': [Errno 2] No such file or directory


    次にzipでダウンロードしたフォルダをバラして実行してみましたが以下のエラーになりました。
    File "WikiExtractor.py", line 66, in <module>
    from .extract import Extractor, ignoreTag, define_template, acceptedNamespaces
    ImportError: attempted relative import with no known parent package

    (WikiExitractor.pyは,確かに存在しており、同じ階層に置いております。)


    *その他にも、https://github.com/attardi/wikiextractor の usage に記述されているような
    pip install wikiextractor
    python -m wikiextractor.WikiExtractor --no-templates -o articles -b 100M ファイルパス
    でも 実行しましたが、以下のエラーが出ました。
    TypeError: cannot pickle '_io.TextIOWrapper' object

    少しわかりづらいかもしれませんが、もし分かることがあれば教えて頂きたいです!
  4. 上の解答本文に追記いたしました。
    私の試したことと、質問者様の実際に試されたことを比較してみてください。

    まず、少なくとも「実行の方法」が異なるようです。
    (私は、プログラマの指示の通り行っただけなので、なぜそうしたのかは説明できません。)

    あとは、展開したフォルダの構造が異なるかも知れません。
    さらには、ダンプファイルも異なる可能性があります。
    あるいは、環境の違いもあるでしょう。

    適切にお答えするためには、質問者様の環境や試されたことについて、さらに詳しい情報が必要になるだろうと思います。
  5. @ryo06

    Questioner
    遅くなりすみません。
    python3.7の仮想環境で追記いただきました実行方法で試した結果、エラーなく処理を実行することができました!
    わざわざご丁寧に有難うございました!
  6. 解決できて良かったです。

Your answer might help someone💌