【问题标题】:How to extract information from ODP accurately? [closed]如何准确地从ODP中提取信息? [关闭]
【发布时间】:2013-10-14 16:16:40
【问题描述】:

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

我听说 Google 会从 ODP (Open Directory Project) 获取页面描述,以防 Google 无法使用页面中的元数据找出描述...我想做类似的事情。

ODP 是来自 Mozilla 的在线目录,其中包含网络上的页面描述,因此我想从 ODP 获取我的搜索结果的描述。如何从 ODP 获取特定 url 的准确描述,如果找不到,则返回 python 类型“None”(这意味着 ODP 不知道我在寻找哪个页面)?

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

【问题讨论】:

    标签: python search-engine


    【解决方案1】:

    要使用 ODP 数据,您需要 download the RDF data dump。 RDF 是一种 XML 格式;您将索引该转储以将 url 映射到描述;我会为此使用 SQL 数据库。

    请注意,URL 可以出现在转储中的多个位置。例如,堆栈溢出列出了两次。 Google 使用来自 this entry 的文本作为网站描述,Bing 使用 this one instead

    数据转储当然相当大。在向数据库添加条目时,使用诸如 ElementTree iterparse() method 之类的合理工具迭代地解析数据集。您实际上只需要查找 <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)
    

    【讨论】:

    • 你的意思是google也下载了rdf转储?如果是这样,我是否必须坐下来多次下载以进行更新? @Martijn Pieters
    • @user2335164:你会定期下载它,是的。最近的转储是在 4 月 28 日创建的。
    • 非常感谢!你能告诉我如何使用从python下载的rdf转储..如果你这样做,我会接受你的回答@Martijn Pieters
    • @user2335164 下载后可以使用this parser做进一步处理。
    • @MartijnPieters 我应该在您在答案中提供的链接中选择哪个下载?
    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多