C#-HTML敏捷包-节点集合-循环表行,选择第n个子项(2)

C#-HTML敏捷包-节点集合-循环表行,选择第n个子项(2),c#,html-agility-pack,C#,Html Agility Pack,我正在与htmlAgilityPack合作,我正在从一个网站上抓取一张桌子 如何修改此选项以仅为每行、每秒钟的列返回值 public static void SearchAnimal(string param) { string prm = param; string url = "http://xxx/xxx.action?name="; //HttpWebRequest request = (HttpWe

我正在与htmlAgilityPack合作,我正在从一个网站上抓取一张桌子

如何修改此选项以仅为每行、每秒钟的列返回值

public static void SearchAnimal(string param)
        {
            string prm = param;
            string url = "http://xxx/xxx.action?name=";
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url+prm);
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //StreamReader stream = new StreamReader(response.GetResponseStream());
            //string final_response = stream.ReadToEnd();
            var webGet = new HtmlWeb();
            var doc = webGet.Load(url + prm);

            HtmlNodeCollection tr = doc.DocumentNode.SelectNodes("//table[@id='animal']//tbody//tr//td");

                  for(int i = 0; i <= tr.Count; ++i){
                    var link = tr
                       .Descendants("a")
                       .First(x => x.Attributes["href"] != null);
                    string hrefValue = link.Attributes["href"].Value;
                    string name = link.InnerHtml;
                    Match match = Regex.Match(hrefValue, @"(\d+)$");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("Result " + tr + ":");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Animal Name: " + name);
                    Console.WriteLine("Animal Key: " + match.Value);
                    Console.WriteLine("-------------------------");
                    Console.WriteLine("");

                       }



        }
公共静态void SearchAnimal(字符串参数)
{
字符串prm=参数;
字符串url=”http://xxx/xxx.action?name=";
//HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url+prm);
//HttpWebResponse=(HttpWebResponse)request.GetResponse();
//StreamReader stream=新的StreamReader(response.GetResponseStream());
//字符串final_response=stream.ReadToEnd();
var webGet=new HtmlWeb();
var doc=webGet.Load(url+prm);
HtmlNodeCollection tr=doc.DocumentNode.SelectNodes(//table[@id='animal']//tbody//tr//td”);
对于(int i=0;i x.Attributes[“href”]!=null);
字符串hrefValue=link.Attributes[“href”].Value;
字符串名称=link.InnerHtml;
Match=Regex.Match(hrefValue,@“(\d+)$”;
Console.ForegroundColor=ConsoleColor.DarkGray;
Console.WriteLine(“结果”+tr+“:”);
Console.ForegroundColor=ConsoleColor.Gray;
Console.WriteLine(“动物名称:”+名称);
Console.WriteLine(“动物键:+match.Value”);
Console.WriteLine(“---------------------------”);
控制台。写线(“”);
}
}

您可以使用XPath位置筛选器从每个
中只获取第二个子

//table[@id='animal']//tbody//tr/td[2]
它实际上等于CSS
:nth-of-type()
选择器,并且仅当所有子项都是同一类型时(在这种情况下,意味着所有子项都是
)才会显示与
:nth-child()
相同的输出