我读过HTMLAgility 1.4是一个很好的解决方案来抓取网页。作为一名新程序员,我希望能够在这个项目上得到一些意见。 我正在做这个作为一个C#申请表格。我正在使用的页面非常直观。我需要的信息仅限于2个标签和 之间。我的目标是将Part-Num,Manu-Number,Description,Manu-Country,Last Modified,Last Modified By的数据拉出页面并将数据发送到一个sql表。一个麻烦的是,还有一个小的PNG图片,也需要从src =“/ partcode/number中抓取。用C#和HTMLAgility刮去网页
我没有任何完整的代码,我认为这段代码会告诉我如果我正朝着正确的方向前进,即使进入调试阶段,我也看不出它做了什么,有人可能会指出我正确的方向,越详细越好,因为很明显我有很多学习谢谢你,我会很感激它
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();
}
}
}
}
该网站的代码是:。
<!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>
如果您需要使用您提供的HTML代码的工作代码,请参阅我的答案。 – 2011-03-28 00:11:08