2011-03-16 71 views
5

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

如果您需要使用您提供的HTML代碼的工作代碼,請參閱我的答案。 – 2011-03-28 00:11:08

回答

5

看看這篇文章對4GuysFromRolla

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

這是我作爲與HTML敏捷性包我的出發點文章,它的工作很大。我相信,您將從本文中獲得所需的全部信息,以執行您嘗試完成的任務。

5

開始部分熄滅:

HtmlDocument doc = new HtmlDocument(); 
doc.LoadHtml("http://localhost"); 

LoadHtml(html)加載HTML字符串到文檔中,我想你想這樣的事情,而不是:

HtmlWeb htmlWeb = new HtmlWeb(); 
HtmlDocument doc = htmlWeb.Load("http://stackoverflow.com"); 
4

的工作準則,根據HTML您提供的來源。它可以因式分解,並且我不檢查null值(在rows,cells中,以及case中的每個值)。如果您有127.0.0.1中的頁面,那就行了。只需將其粘貼到控制檯應用程序Main方法中,並嘗試瞭解它。

HtmlDocument doc = new HtmlWeb().Load("http://127.0.0.1");  

var rows = doc.DocumentNode.SelectNodes("//table[@class='data']/tr"); 
foreach (var row in rows) 
{ 
    var cells = row.SelectNodes("./td"); 
    string title = cells[0].InnerText; 
    var valueRow = cells[2]; 
    switch (title) 
    { 
     case "Part-Num": 
      string partNum = valueRow.SelectSingleNode("./img[@alt]").Attributes["alt"].Value; 
      Console.WriteLine("Part-Num:\t" + partNum); 
      break; 
     case "Manu-Number": 
      string manuNumber = valueRow.SelectSingleNode("./img[@alt]").Attributes["alt"].Value; 
      Console.WriteLine("Manu-Num:\t" + manuNumber); 
      break; 
     case "Description": 
      string description = valueRow.InnerText; 
      Console.WriteLine("Description:\t" + description); 
      break; 
     case "Manu-Country": 
      string manuCountry = valueRow.InnerText; 
      Console.WriteLine("Manu-Country:\t" + manuCountry); 
      break; 
     case "Last Modified": 
      string lastModified = valueRow.InnerText; 
      Console.WriteLine("Last Modified:\t" + lastModified); 
      break; 
     case "Last Modified By": 
      string lastModifiedBy = valueRow.InnerText; 
      Console.WriteLine("Last Modified By:\t" + lastModifiedBy); 
      break; 
    } 
} 
+0

感謝你的這個例子,它教會了我更多關於使用html敏捷性的知識。如果您對挑戰感興趣,我有另一個頁面正在從中獲取數據,但沒有典型佈局。我很想看看你會如何處理這種情況。 – JRB 2011-03-31 20:11:29

+2

@JRB嗯,我建議你先嚐試一下,如果你在使用它的時候遇到了問題,那麼把它作爲一個問題發佈,然後嘗試做什麼,我們會盡力幫助你。 – 2011-03-31 22:49:21