まずについてが含まれていること、名前に名前を変更している、WARC、またはWebアーカイブは、Webページのアーカイブ形式です。 warc
ファイルを読むには、特殊なヘッダーが含まれているため、少しトリッキーです。 warc
ファイルがthis formatであると仮定します。
次のコードを使用すると、メタデータとコンテンツを含むすべてのレコードの辞書を読み込み、解析して返すことができます。
def read_header(file_handler):
header = {}
line = next(file_handler)
while line != '\n':
key, value = line.split(': ', 1)
header[key] = value.rstrip()
line = next(file_handler)
return header
def warc_records(path):
with open(path) as fh:
while True:
line = next(fh)
if line == 'WARC/1.0\n':
output = read_header(fh)
if 'WARC-Refers-To' not in output:
continue
output["Content"] = next(fh)
yield output
あなたは次のように辞書にアクセスすることができます。
records = warc_records("<some path>')
>>> next_record = next(records)
>>> sorted(next_record.keys())
['Content', 'Content-Length', 'Content-Type', 'WARC-Block-Digest', 'WARC-Date', 'WARC-Record-ID', 'WARC-Refers-To', 'WARC-Target-URI', 'WARC-Type', 'WARC-Warcinfo-ID']
>>> next_record['WARC-Date']
'2013-06-20T00:32:15Z'
>>> next_record['WARC-Target-URI']
'http://09231204.tumblr.com/post/44534196170/high-res-new-photos-of-the-cast-of-neilhimself'
>>> next_record['Content'][:30]
'Side Effects high res. New pho'
それはあなたが解決策を持っているにリンクされている質問への受け入れ答えのように見えます。それを試しましたか? – cco
@ccoコードの最初のボックスは印刷されません。 – user3487667