用python读取warc文件

用python读取warc文件,python,warc,Python,Warc,我想读一个warc文件,我写了下面的代码,但是没有打印出来 >>import warc >>f = warc.open("01.warc.gz") >>for record in f: print record['WARC-Target-URI'], record['Content-Length'] 然而,当我编写下面的命令时,我得到了结果 >>print f <warc.warc.WARCFile instance at 0x00

我想读一个warc文件,我写了下面的代码,但是没有打印出来

>>import warc
>>f = warc.open("01.warc.gz")
>>for record in f:
    print record['WARC-Target-URI'], record['Content-Length']
然而,当我编写下面的命令时,我得到了结果

>>print f
<warc.warc.WARCFile instance at 0x0000000002C7DE88>
>打印f

请注意,我的warc文件是来自Clueweb09数据集的文件之一。我提到它是因为。

我和你有同样的问题

在对模块进行了一些研究之后,我找到了一个解决方案

尝试使用
record.payload.read()
,以下是完整示例:

import warc
f = warc.open("01.warc.gz")
for record in f:
  print record.payload.read()
另外,我可以说,您不仅可以读取
warc
文件,还可以读取
wet
文件。小技巧是将其重命名为name,其中包含
.warc


首先,WARC或Web存档是一种网页存档格式。 读取
warc
文件有点棘手,因为它包含一些特殊的头。 假设您的
warc
文件为

您可以使用以下代码为每个包含元数据和内容的记录加载、解析和返回字典

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'
records=warc\u记录(“)
>>>下一条记录=下一条(记录)
>>>已排序(next_record.keys())
[“内容”、“内容长度”、“内容类型”、“WARC块摘要”、“WARC日期”、“WARC记录ID”、“WARC引用”、“WARC目标URI”、“WARC类型”、“WARC Warcinfo ID”]
>>>下一条记录['WARC-Date']
“2013-06-20T00:32:15Z”
>>>下一个_记录['WARC-Target-URI']
'http://09231204.tumblr.com/post/44534196170/high-res-new-photos-of-the-cast-of-neilhimself'
>>>下一条记录[内容][:30]
“副作用高分辨率新pho”

您链接的问题的公认答案似乎有解决方案。您尝试过吗?@cco第一个代码框不打印。