如何准确地从ODP中提取信息?[关闭]

发布于 2021-01-29 15:03:26

在这里很难说出要问什么。这个问题是模棱两可,含糊,不完整,过于宽泛或夸张的,不能以目前的形式合理地回答。如需帮助澄清此问题以便可以重新打开,
请访问帮助中心

7年前关闭。

我正在用python构建搜索引擎。

我听说Google会从ODP(开放目录项目)中获取页面的描述,以防Google无法使用页面中的元数据来找出描述…我想做类似的事情。

ODP是Mozilla的在线目录,其中包含对网页的描述,因此我想从ODP中获取搜索结果的描述。如何从ODP中获取特定网址的准确描述,并在找不到的情况下返回python类型“
None”(这意味着ODP不知道我要查找的页面)?

PS。有一个名为http://dmoz.org/search?q=Your+Search+Params的网址,但我不知道如何从那里提取信息。

关注者
0
被浏览
49
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    要使用ODP数据,请下载RDF数据转储。RDF是XML格式;您将索引转储以将URL映射到描述;我将为此使用SQL数据库。

    请注意,URL可以存在于转储中的多个位置。例如,堆栈溢出被两次列出。Google使用此条目中的文本作为网站描述,而Bing则使用此文本

    数据转储当然很大。在向数据库添加条目时,使用诸如ElementTreeiterparse()方法之类的明智工具来迭代解析数据集。您实际上只需要查找<ExternalPage>元素,<d:Title>并在和<d:Description>下方添加条目。

    使用lxml(更快,更完整的ElementTree实现)如下所示:

    from lxml import etree as ET
    import gzip
    import sqlite3
    
    conn = sqlite3.connect('/path/to/database')
    
    # create table
    with conn:
        cursor = conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS odp_urls 
            (url text primary key, title text, description text)''')
    
    count = 0
    nsmap = {'d': 'http://purl.org/dc/elements/1.0/'}
    with gzip.open('content.rdf.u8.gz', 'rb') as content, conn:
        cursor = conn.cursor()
        for event, element in ET.iterparse(content, tag='{http://dmoz.org/rdf/}ExternalPage'):
            url = element.attrib['about']
            title = element.xpath('d:Title/text()', namespaces=nsmap)
            description = element.xpath('d:Description/text()', namespaces=nsmap)
            title, description = title and title[0] or '', description and description[0] or ''
    
            # no longer need this, remove from memory again, as well as any preceding siblings
            elem.clear()
            while elem.getprevious() is not None:
                del elem.getparent()[0]
    
            cursor.execute('INSERT OR REPLACE INTO odp_urls VALUES (?, ?, ?)',
                (url, title, description))
            count += 1
            if count % 1000 == 0:
                print 'Processed {} items'.format(count)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看