2011-04-19 51 views
-3

可能重複:
links from web page給予計數鏈接

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using HtmlAgilityPack; 
using System.IO; 


namespace test 
{ 
    class Program : System.Web.UI.Page 
    { 
     static void Main(string[] args) 
     { 
      List<string> hrefTags = new List<string>(); 

      var webGet = new HtmlWeb(); 
      var doc = webGet.Load("http://en.wikipedia.org/wiki/Myspace"); 

      foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) 
{ 
HtmlAttribute att = link.Attributes["href"]; 
//hrefTags.Add(att.Value); 

    string href = att.Value; 
string text = link.InnerText; 

Console.WriteLine(text); 
// Console.WriteLine(att.Value); 
Console.WriteLine(href); 


      } 
      Console.ReadKey(); 

     } 
    } 
} 

此代碼給我一個網頁的所有鏈接。我想統計這些鏈接,我想爲每個鏈接分配一個數字。我怎樣才能做到這一點?

+0

這是一個確切的副本。儘管如此,要給這個傢伙點堅持。當然,我必須拿走這些點,因爲他顯然沒有第一次學習。 = P – Pete 2011-04-19 02:12:10

+0

請不要一遍又一遍地提出同樣的問題。 – 2011-04-19 02:12:38

回答

0
int i = 1; 
foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) 
{ 
    //All your code here - output i where you want the number to be 
    i++; 
} 
+0

我不想在每個鏈接文本之前像1個Home/wiki/home然後2個聯繫人 – adeel 2011-04-19 13:37:46

0
int linkCount = 0; 
foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) 
{ 
    HtmlAttribute att = link.Attributes["href"]; 

    string href = att.Value; 
    string text = link.InnerText; 

    Console.WriteLine("Link {0}", ++linkCount); 
    Console.WriteLine(text); 
    Console.WriteLine(href); 
} 
Console.WriteLine ("{0} links found", linkCount); 
0

可以使用System.Tuple類存儲列表中的這些項目,即:

List<Tuple<int, string, string>> links = new List<Tuple<int, string, string>>(); 
int linkCount = 0; 

foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) 
{ 
    HtmlAttribute att = link.Attributes["href"]; 
    string href = att.Value; 
    string text = link.InnerText; 

    links.Add(Tuple.Create(linkCount++, href, text)); 
} 

links系列將包含所有你就可以處理。但是你喜歡你的鏈接。如果你不希望使用Tuple,你可以創建一個看起來是這樣的一類:

public class Link 
{ 
    public int Index { get; set; } 
    public string Url { get; set; } 
    public string Text { get; set; } 
} 
0

下面的工作:

class Program : System.Web.UI.Page 
{ 
    static void Main(string[] args) 
    { 
     List<string> hrefTags = new List<string>(); 

     var webGet = new HtmlWeb(); 
     var doc = webGet.Load("http://en.wikipedia.org/wiki/Myspace"); 

     List<string> links = new List<string>(); 
     List<string> texts = new List<string>(); 

     foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) 
     { 
      links.Add(link.Attributes["href"].Value); 
      texts.Add(link.InnerText); 
     } 

     Console.WriteLine("Total of links found: {0}", links.Count); 

     for (int i = 0; i < links.Count; i++) 
     { 
      Console.WriteLine("-- Link {0} --", i+1); 
      Console.WriteLine("Text: {0}", texts[i]); 
      Console.WriteLine("Link: {0}", texts[i]): 
     } 

     Console.ReadKey(); 

    } 
} 

重要:
當你這樣做:

doc.DocumentNode.SelectNodes("//a[@href]") 

如果找不到結果,它將返回null,所以你應該這樣做:

HtmlNodeCollection linkNodes = doc.DocumentNode.SelectNodes("//a[@href]"); 
if (linkNodes != null) 
{ 
    foreach (HtmlNode link in linkNodes) 
    { 
     //The same here... 
    } 
}