【发布时间】:2011-07-16 06:26:26
【问题描述】:
我读到HTMLAgility 1.4 是抓取网页的绝佳解决方案。作为一名新程序员,我希望我能在这个项目上得到一些意见。
我这样做是作为C# 申请表。我正在使用的页面相当简单。我需要的信息被困在两个标签 <table class="data"> 和 </table> 之间。
我的目标是将Part-Num、Manu-Number、Description、Manu-Country、Last Modified、Last Modified By 的数据拉出页面并将数据发送到SQL 表。
一个转折点是,还有一张PNG 的小图片也需要从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();
}
}
}
网页代码是:
<!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>
<head/>
</html>
【问题讨论】:
-
如果您想要使用您提供的 HTML 代码的工作代码,请查看我的答案。
标签: c# .net web-scraping html-agility-pack data-mining