前提・実現したいこと
ローカルにWikipediaのダンプを保存して、それの処理をしています。時間がかかるので早くやりたいです!
http://taka-say.hateblo.jp/entry/2016/05/20/221817
こちらのブログの内容の通り、Wikipediaのデータを落としてきてローカルに保存しました。
ファイルは約500KBごとに分割されていて、全部で約2.96GBあります。
中身はブログに書いてある通り、<doc>というタグでくくられたxmlみたいなファイルです。
その中から、BeautifulSoupを使って、指定したキーワードの記事を抽出するPythonを書きました。
以下、動作します!
Python
1import os 2from bs4 import BeautifulSoup 3import time 4 5def files(path): 6 for pathname, dirnames, filenames in os.walk(path): 7 for filename in filenames: 8 yield os.path.join(pathname, filename) 9 10starttime = time.time() 11cd = 'ぱす〜〜/Wikipedia_dump190121/extracted/' 12q = '恋するフォーチュンクッキー' 13for path in files(cd): 14 if 'wiki_' in path: 15 with open(path) as f: 16 soup = BeautifulSoup(f, 'html.parser') 17 article = soup.find_all('doc', title=q) 18 if article: 19 print(article) 20endtime = time.time() 21print(endtime-starttime, ' sec')
しかしめちゃくちゃ遅くて、さっきやったところ105秒もかかりました。
1秒ぐらいでできたらうれしいなと思っているんですが、方法はありますでしょうか?
試したこと
ファイルの開閉に時間がかかるからかな?と思い、上記ブログの通りぜんぶファイルをつなげました。
find extracted -name 'wiki*' -exec cat {} \; > text.xml
xmlファイルひとつにまとまりました。約2.81GBあります。
上記Pythonと同じような感じで処理させてみたところ、下記のようにエラーになりました。重すぎるからかも…。
OSError Traceback (most recent call last) <ipython-input-53-b974053833d4> in <module>() 4 q = '恋するフォーチュンクッキー' 5 with open('ぱす〜〜/Wikipedia_dump190121/jawiki-latest-pages-articles.xml.bz2') as f: ----> 6 soup = BeautifulSoup(f, 'html.parser') 7 article = soup.find_all('doc', title=q) 8 if article: /usr/local/lib/python3.6/site-packages/bs4/__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs) 189 190 if hasattr(markup, 'read'): # It's a file-type object. --> 191 markup = markup.read() 192 elif len(markup) <= 256 and ( 193 (isinstance(markup, bytes) and not b'<' in markup) OSError: [Errno 22] Invalid argument
補足情報(FW/ツールのバージョンなど)
Python 3.6.5
jupyter 4.4.0
ストレージはSSDです。
ほかに必要な情報あれば教えてください…。
回答2件
あなたの回答
tips
プレビュー