我讀過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