2017-04-27 47 views
0

在使用Python包裝器構建Onionscan Scraper之後,我最近試圖使用Onionscantutorial。自從編寫教程以來,Onionscan已經從將所有數據存儲在JSON文件中轉移到將其中一些存儲在使用Tiedot構建的數據庫中。使用Python從Onionscan讀取原始數據(二進制?)Tiedot(NOSQL/JSON)數據

我想找到一種方法,將其中一個沒有擴展名的文件取名爲dat_0並使用Python對其進行解析。

在MacOS上的TextEdit我得到以下看着dat_0 ... dat_0 as displayed by Textedit

雖然崇高的文本顯示爲... dat_0 as displayed in Sublime text

我一直在試圖找出如何解析這個文件使用Python。我假設Tiedot的文檔和通過Textedit的視圖使用JSON結構,但我沒有太多的運氣。

import json 
f = open('crawls/dat_0','rb') 
data = json.dumps(f.read()) 

IOPub data rate exceeded. 
The notebook server will temporarily stop sending output 
to the client in order to avoid crashing it. 
To change this limit, set the config variable 
`--NotebookApp.iopub_data_rate_limit`. 

data = json.loads(f.read()) 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-5-21c335a72c11> in <module>() 
----> 1 data = json.loads(f) 

/Users/James/anaconda/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 
    310  if not isinstance(s, str): 
    311   raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312        s.__class__.__name__)) 
    313  if s.startswith(u'\ufeff'): 
    314   raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)", 

TypeError: the JSON object must be str, not 'bytes' 

有誰...... 一)知道這個文件是什麼? b)知道如何成功地解析它以使數據在Python中可用?

+0

爲什麼會出現在你的問題中去標籤?您在Python中尋求幫助,事實上OnionScan是用Go編寫的,與您的問題無關。 –

+0

@WilliamPoussier我並不完全確定我試圖解析的數據是否是Go社區中相當標準的格式,因爲它是使用Tiedot生成的。我認爲如果是這樣的話,那些熟悉Go的人會很快得到認可,但不一定是那些熟悉Python的人。 –

回答

1

對於任何未來發現此問題的人,我自己找到了解決方案。我使用Kaitai Struct爲Tiedot文件結構創建二進制解析器。開泰可以爲語言的大量創造解析器所以這是一個相當有用的工具

的開泰結構我用來生成解析器是...

meta: 
    id: parser 
seq: 
    - id: records 
    type: record 
    repeat: eos 
types: 
    record: 
    seq: 
    - id: validity 
     type: s1 
    - id: allocated 
     type: s8le 
    - id: document 
     type: str 
     encoding: utf-8 
     terminator: 1 
     eos-error: false