版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#搜索引擎开发解析HTML的两种方法C#解析Html的第一种方法:用System.Net.WebClient下载Web Page存到本地文件或者String中,用正则表达式来分析。这个方法可以用在Web Crawler等需要分析很多Web Page的应用中。估计这也是大家最直接,最容易想到的一个方法。转自网上的一个实例:所有的href都抽取出来:1. using System; 2. using System.Net; 3. using System.Text; 4. using System.Text.Regula
2、rExpressions; 5. namespace HttpGet 6. 7. class Class1 8. 9. STAThread 10. static void Main(string
3、160;args) 11. 12. System.Net.WebClient client = new WebClient(); 13.
4、byte page = client.DownloadData(""); 14. string content = System.Text.Encoding.UTF8.GetString(page); 15.
5、;string regex = "href="'(http:/|./|/)?w+(.w+)*(/w+(.w+)?)*(/|?w*=w*(&w*=w*)*)?"'" 16. Regex re = new Regex(regex); 17.
6、 MatchCollection matches = re.Matches(content); 18. 19. System.Collections.IEnumerator enu = matches.GetEnumerator(); 20.
7、; while (enu.MoveNext() && enu.Current != null) 21. 22. &
8、#160; Match match = (Match)(enu.Current); 23. Console.Write(match.Value + "rn"); 24. &
9、#160; 25. 26. 27. 一些爬虫的HTML解析中也是用的类似的方法。C#解析Html的第二种方法:利用Winista.Htmlparser.Net 解析Html。这是.NET平台下解析Html的开源代码,网上有源码下载,百度一下就能搜到,这里就不提供了。并且有英文的帮助文档。找不到的留下邮箱。个人认为这是.net平台下解析html不错的解决方案,基本上能够满足我们对
10、html的解析工作。自己做了个实例:28. using System; 29. using System.Collections.Generic; 30. using System.ComponentModel; 31. using System.Data; 32. using System.Drawing; 33. using System.Linq; 34. using System.Text; 35. using System
11、.Windows.Forms; 36. using Winista.Text.HtmlParser; 37. using Winista.Text.HtmlParser.Lex; 38. using Winista.Text.HtmlParser.Util; 39. using Winista.Text.HtmlParser.Tags; 40. using Winista.Text.HtmlParser.Filters; 41. 42. 43. na
12、mespace HTMLParser 44. 45. public partial class Form1 : Form 46. 47. public Form1() 48. &
13、#160;49. InitializeComponent(); 50. AddUrl(); 51. 52. 53. &
14、#160; private void btnParser_Click(object sender, EventArgs e) 54. 55. #region 获得网页的html 56.
15、60; try 57. 58. 59. txtHtmlWhole.Text = ""
16、0;60. string url = CBUrl.SelectedItem.ToString().Trim(); 61. System.Net.WebClient
17、aWebClient = new System.Net.WebClient(); 62. aWebClient.Encoding = System.Text.Encoding.Default; 63.
18、 string html = aWebClient.DownloadString(url); 64. txtHtmlWhole.Text = html; 65.
19、60; 66. catch (Exception ex) 67. 68.
20、 MessageBox.Show(ex.Message); 69. 70. #endregion 71. 72.
21、60; #region 分析网页html节点 73. Lexer lexer = new Lexer(this.txtHtmlWhole.Text); 74. Parser parser =
22、60;new Parser(lexer); 75. NodeList htmlNodes = parser.Parse(null); 76. this.treeView1.Nodes.Clear(); 77. &
23、#160; this.treeView1.Nodes.Add("root"); 78. TreeNode treeRoot = this.treeView1.Nodes0; 79.
24、; for (int i = 0; i < htmlNodes.Count; i+) 80. 81.
25、160;this.RecursionHtmlNode(treeRoot, htmlNodesi, false); 82. 83. 84. #endregion 85. 86.
26、60; 87. 88. private void RecursionHtmlNode(TreeNode treeNode, INode htmlNode, bool siblingRequired) 89. 90.
27、 if (htmlNode = null | treeNode = null) return; 91. 92. TreeNode current = treeNode; 93. &
28、#160; TreeNode content 94. /current node 95. if (htmlNode is
29、;ITag) 96. 97. ITag tag = (htmlNode as ITag); 98.
30、160; if (!tag.IsEndTag() 99. 100.
31、60; string nodeString = tag.TagName; 101. if (tag.Attributes != null && tag.Attributes.Count > 0
32、) 102. 103. if (ta
33、g.Attributes"ID" != null) 104. 105.
34、60; nodeString = nodeString + " id="" + tag.Attributes"ID".ToString() + "" " 106.
35、60; 107. if (tag.Attributes"HREF"
36、 != null) 108. 109.
37、; nodeString = nodeString + " href="" + tag.Attributes"HREF".ToString() + "" " 110. &
38、#160; 111. 112. &
39、#160; 113. current = new TreeNode(nodeString); 114.
40、; treeNode.Nodes.Add(current); 115. 116. 117. 1
41、18. /获取节点间的内容 119. if (htmlNode.Children != null && htmlNode.Children.Count > 0) 120.
42、 121. this.RecursionHtmlNode(current, htmlNode.FirstChild, true); 122.
43、60; content = new TreeNode(htmlNode.FirstChild.GetText(); 123. treeNode.Nodes.Add(content); 124.
44、60; 125. 126. /the sibling nodes 127. if (siblingRequired) 128.
45、; 129. INode sibling = htmlNode.NextSibling; 130. while (sibling != null
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中职教育二年级上学期装备制造《电冰箱的认识与选择》教学课件
- 幼师一日工作流程
- 电缆电力施工组织设计
- 江苏省火电企业集控值班员技能竞赛理论考试题库(含答案)
- 第十八届“振兴杯”全国青年职业技能大赛(钳工赛项)决赛试题库-上(选择题)
- 2024年全国职业院校技能大赛中职组(养老照护赛项)考试题库-下(判断题)
- 2024年全国职业院校技能大赛高职组(检验检疫技术赛项)考试题库(含答案)
- (新版)财富管理业务从业人员认证考试题库(浓缩500题)
- 校园安全专项整治工作活动总结
- 湖孝感市2024年高三9月起点调研考试 语文试卷(含答案)
- 山鬼课件上课用课件
- 译林版九年级英语上册全册完整课件
- 安全风险分析及预警记录表
- 保洁知识考试题库250题(含答案)
- 家庭经济困难学生认定申请表(模板)
- 2019武汉申报户口登记表
- 中餐摆台课件
- 《国企风险管理》课件
- 房地产物业移交表格全套
- 文物保护目标责任书
- 抚顺市第一中等职业技术专业学校学校规章制度汇编-20-23
评论
0/150
提交评论