使用C#和HTMLAgility搜索网页

JRB*_*JRB 5 .net c# data-mining web-scraping html-agility-pack

我已经读过HTMLAgility 1.4是一个很好的解压缩网页的解决方案.作为一名新程序员,我希望我能对这个项目有所了解.我这样做是作为ac#申请表.我正在使用的页面非常简单.我需要的信息只停留在2个标签之间.我的目标是将Part-Num,Manu-Number,Description,Manu-Country,Last Modified,Last Modified By的数据拉出页面并将数据发送到sql表.一个转折是还有一个小的png pic,也需要从src ="/ partcode/number中获取.

我没有任何已完成的代码.我以为这段代码会告诉我我是否正朝着正确的方向前进.即使进入调试我也看不到它做了什么.有人可能会指出我在这方面的正确方向.越详细越好,因为很明显我需要学习很多东西.谢谢,我真的很感激.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Xml;

namespace Stats
{
    class PartParser
    {
        static void Main(string[] args)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml("http://localhost");//my understanding this reads the entire page in?
            var tables = doc.DocumentNode.SelectNodes("//table");// I assume that this sets up the search for words containing table

        }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.ReadKey();    
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

网络代码是:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Part Number Database: Item Record</title>
<table class="data">
<tr><td>Part-Num</td><td width="50"></td><td><img src="/partcode/number/072140" alt="072140"/></td></tr>
<tr><td>Manu-Number</td><td width="50"></td><td><img src="/partcode/manu/00721408" alt="00721408" /></td></tr>    
<tr><td>Description</td><td></td><td>Widget 3.5</td></tr>
<tr><td>Manu-Country</td><td></td><td>United States</td></tr>    
<tr><td>Last Modified</td><td></td><td>26 Jan 2009,  8:08 PM</td></tr>    
<tr><td>Last Modified By</td><td></td><td>Manu</td></tr>
</table>
<p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Rob*_*ams 6

查看4GuysFromRolla上的这篇文章

http://www.4guysfromrolla.com/articles/011211-1.aspx

这是我用HTML Agility Pack作为起点的文章,它的效果非常好.我相信您将从本文中获得所需的所有信息,以执行您尝试完成的任务.


Bro*_*ass 6

开始部分是关闭的:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("http://localhost");   
Run Code Online (Sandbox Code Playgroud)

LoadHtml(html) 将html字符串加载到文档中,我想你想要这样的东西:

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc  = htmlWeb.Load("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)