2013-08-04 32 views
2

我目前正在研究一个项目,该项目涉及查找与某个关键字相关的'知识领域'。我打算使用DMOZ来做到这一点。 例如, '布拉德·皮特' 给解析用于Python中的类别查询的DMOZ转储

Arts: People: P: Pitt, Brad: Fan Pages (10) 

Arts: People: P: Pitt, Brad: Articles and Interviews (5) 

Arts: People: P: Pitt, Brad (4) 

Arts: People: P: Pitt, Brad: Image Galleries (2) 

Arts: People: P: Pitt, Brad: Movies (2) 

等等...

我从DMOZ网站structure.rdf.u8转储。有人向我提到,如果我不需要网址,只需要这个文件就足够了(我不需要这些网站,只需要与关键字有关的类别)。或者我还需要内容文件吗?

此外,我想知道使用Python(任何库)解析结构文件的最佳方法。我对XML没有任何认识,尽管我对Python很好。

回答

1

我开始与https://github.com/kremso/dmoz-parser 并做了一个简单的话题过滤器: https://github.com/lawrencecreates/dmoz-parser/blob/master/sample.py#L6

class LawrenceFilter: 
    def __init__(self): 
    self._file = open("seeds.txt", 'w') 

    def page(self, page, content): 
     if page != None and page != "": 
      topic = content['topic'] 
      if topic.find('United_States/Kansas/Localities/L/Lawrence') > 0 : 
       self._file.write(page + "\n") 
       print "found page %s in topic %s" % (page , topic) 

    def finish(self): 
    self._file.close()