栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

数据转储当然很大。在向数据库添加条目时,使用诸如ElementTree

iterparse()
方法之类的明智工具来迭代解析数据集。您实际上只需要查找
<ExternalPage>
元素,
<d:Title>
并在和
<d:Description>
下方添加条目。

使用

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

from lxml import etree as ETimport gzipimport sqlite3conn = sqlite3.connect('/path/to/database')# create tablewith conn:    cursor = conn.cursor()    cursor.execute('''        CREATE TABLE IF NOT EXISTS odp_urls         (url text primary key, title text, description text)''')count = 0nsmap = {'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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号