0

我必须分析一下commoncrawl。为此,我使用的是 python 2.7。我观察了一些warc文件,warc.gz文件中有一些二进制数据。我必须使用 bs4 解析 html 源代码。但是我怎么能检测到这是文本数据,这是二进制的。例如,有一个包含二进制数据的 URL 记录。http://aa-download.avg.com/filedir/inst/avg_free_x86_all_2015_5315a8160.exe

我如何跳过二进制数据并在 python 中只处理文本数据?

4

1 回答 1

0

你可以使用python-magic来识别东西。

In [1]: import magic

In [2]: magic.from_file('places.sqlite')
Out[2]: b'SQLite 3.x database, user version 33, last written using SQLite version 3015001'

In [3]: magic.from_file('installed-port-list.txt')
Out[3]: b'ASCII text'

In [4]: magic.from_file('quotes.gz')
Out[4]: b'gzip compressed data, was "quotes", last modified: Tue Dec  6 20:35:44 2016, from Unix'

请注意,虽然这些示例使用该from_file函数,但 python-magic 也有一个from_buffer函数。

于 2017-01-13T12:45:58.587 回答