0

我正在尝试从此处获取所有名称的列表(表中的冠军链接标题),但我没有成功.. 谁能指导我这段代码有什么问题?

谢谢!

var url = "http://leagueoflegends.wikia.com/wiki/List_of_champions";
var web = new HtmlWeb();
var doc = web.Load(url);            

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table[3]/tr"))
{
    HtmlNode item = table.SelectSingleNode("//a");
    Console.WriteLine(item.GetAttributeValue("title", false));
}

更新:

好吧,我让它与这段代码一起工作得很好:

var url = "http://leagueoflegends.wikia.com/wiki/List_of_champions";
var web = new HtmlWeb();
var doc = web.Load(url);            

foreach (HtmlNode item in doc.DocumentNode.SelectNodes("//table[3]/tr/td/span/a"))
{
    Console.WriteLine(item.Attributes["title"].Value);
}

return true;

谢谢你的帮助!

4

2 回答 2

1

请以这种方式使用xpath

foreach (HtmlNode linkItem in doc.DocumentNode.SelectNodes("//table[3]/tr//a"))
{
    Console.WriteLine(linkItem.Attributes["title"].Value());
    Console.WriteLine(linkItem.Attributes["alt"].Value());
}
于 2012-07-19T13:13:39.220 回答
1

我敲了一个快速而肮脏的例子,经过测试并且可以完美运行,但是您需要对结果进行一些格式化:

protected void Page_Load(object sender, EventArgs e)
{
      List<HtmlAgilityPack.HtmlNode> test = GetInnerTest();

      foreach (var node in test)
      {
            Response.Write("Result: " + node.InnerHtml.ToString());
      }

}

public List<HtmlAgilityPack.HtmlNode> GetInnerTest()
{
     HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

     doc.OptionFixNestedTags = true;
     doc.Load(requestData("http://leagueoflegends.wikia.com/wiki/List_of_champions"));

     var node = doc.DocumentNode.Descendants("span").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("character_icon")).ToList();

     return node;
}


public StreamReader requestData(string url)
{
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
       HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

       StreamReader sr = new StreamReader(resp.GetResponseStream());

       return sr;
}

您需要下载HtmlAgilityPack并包含参考以使其正常工作。

于 2012-07-19T13:31:47.517 回答