python lxml中etree.html和etree.parse有什么区别

python lxml中etree.html和etree.parse有什么区别,第1张

etree.parse直接接受一个文档,按照文档结构解析

import xml.etree.ElementTree as ET

tree = ET.parse('country_data.xml')

root = tree.getroot()

etree.html可以解析html文件:

page = etree.HTML(html.lower().decode('utf-8'))

hrefs = page.xpath(u"//a")

for href in hrefs:

print href.attrib

jquery可以利用parseHtml来 *** 作html字符串:

<html>

<head>  

<script src="

$log = $( "#log" ),    

str = "hello, <b>my name is</b> jQuery.",    

html = $.parseHTML( str ),    

nodeNames = [] // Append the parsed HTML$log.append( html ) // Gather the parsed HTML's node names$.each( html, function( i, el ) {    nodeNames[i] = "<li>" + el.nodeName + "</li>"}) // Insert the node names$log.append( "<h3>Node Names:</h3>" )$( "<ol></ol>" )  .append( nodeNames.join( "" ) )  .appendTo( $log ) 

</script> 

</body>

</html>

运行结果:

Content:

hello, my name is jQuery.Node Names:

#text

B

#text

目前行业网站的全文检索的方式主要有两种

方式一:通过数据库自带的全文索引

方式二:通过程序来自建全文索引系统

以sql server 2005为例

2005本身就自带全文索引功能,你可以先对数据库表

建立索引,具体如何建索引网上搜索一下,建立完索引之后,你就可以用sql来实现检索功能,例如:select * from ytbxw where

contaiins(字段,' 中国')多个查询值之间可以用and 或

or来实现,在单表以及单表视图上建全文索引对2005来说根本不是问题,但在多表视图建全文索引2005目前还无法实现这个功能,拿

www.ytbxw.com为例,其每个栏目的信息都是分开存放的,所以在检索上就无法用该方法来解决这个问题.

下面重点说一下如何用程序来实现检索功能

如果你想自己开发一个全文检索系统,我想这是相当复杂事情,要想实现也不是那么容易的事情,所以在这里我推荐一套开源程序,那就是dotlucene,我想大家可能都听过这个东东吧,那我就讲讲如何来实现多表情况下的全文检索.

1、新建winform项目,把lucene.net.dll添加到该项目中来

2、创建一个类,类名可以自己取

public class indexer

{

private indexwriter writer

 //在指定路径下创建索引文件

public indexer(string directory)

{

writer = new indexwriter(directory, new standardanalyzer(), true)

writer.setusecompoundfile(true)

}

 //将信息添加到索引文件中

/*

field.text:为索引+读取

field.unindexed:不需要做索引

*/

public void addhtmldocument(string path,string title,string content)

{

document doc = new document()

doc.add(field.text("text", content))

doc.add(field.unindexed("path", path))

doc.add(field.text("title", title))

writer.adddocument(doc)

}

//解析html,过滤html代码

private string parsehtml(string html)

{

string temp = regex.replace(html, "<[^>]*>", "")

return temp.replace(" ", " ")

}

//从页面中获取文章标题

private string gettitle(string html)

{

match m = regex.match(html, "<title>(.*)</title>")

if (m.groups.count == 2)

return m.groups[1].value

return "(unknown)"

}

//添加新闻到索引

public void addnews()

{

//从数据库获取记录(这部分略过)

for (int i = 1i <= pagesizei++)

{

rootid = int.parse(dr["classid"].tostring().substring(0, 2))

// 写入索引

addhtmldocument(http://www.ytbxw.com + dr["id"].tostring() + ".html",

dr["title"].tostring(), parsehtml(dr["content"].tostring()))

}/info/

}

//关闭索引

public void close()

{

writer.optimize()

writer.close()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7466187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存