我怎么能得到tr链接和内容与HtmlAgilityPack

本文关键字:HtmlAgilityPack 链接 怎么能 tr | 更新日期: 2023-09-27 18:16:08

我使用下面的代码来获取url的html源:

private string GetUrlSource(string urlAddress)
        {
            string content = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;
                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                content = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
            }
            return content;
        }

那么,使用下面的代码获取数据:

                var source = GetUrlSource(urlAddress);
                var htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(source);
var nodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='linear-view']/table/tr/td");

和我的节点结果是:

<tr>
  <th>Title</th>
  <th>Publisher</th>
</tr>
<tr>
  <td><a href="http://link1.com" id="14.4">Title1</a></td>
  <td>Publisher1</td>
</tr>
<tr>
  <td><a href="http://link2.com" id="12.0">Title2</a></td>
  <td>Publisher2</td>
</tr>
<tr>
  <td><a href="http://link3.com/" id="84.4">Title3</a></td>
  <td>Publisher3</td>
</tr>

我使用下面的代码来获取数据:

foreach (var node in nodes)
                {
                    HtmlNodeCollection rows = node.SelectNodes(".//a");
                    if (rows != null)
                    {
                        for (int j = 0; j < rows.Count; ++j)
                        {
                            var link = rows[j].Attributes["href"].Value;
                            var title = rows[j].InnerText;
                        }
                    }
                    else
                    {
                        var publisher = node.InnerText;
                    }
                }

如何获取链接,标题&每个tr标签的发布者,不带if &其他的吗?例如:http://link1.com , Title1 , Publisher1, http://link2.com , Title2 , Publisher2, http://link3.com , Title3 , Publisher3

我怎么能得到tr链接和内容与HtmlAgilityPack

可能的方法之一:

//select <tr> having child node <td>
var tr = doc.DocumentNode.SelectNodes("//div[@class='linear-view']/table/tr[td]");
foreach (HtmlNode node in tr)
{
    //select <td> having child node <a>
    var td1 = node.SelectSingleNode("./td[a]"); //or using index: ./td[1]
    var link = td1.FirstChild.Attributes["href"].Value;
    var title = td1.InnerText;
    //select <td> not having child node <a>
    var publisher = node.SelectSingleNode("./td[not(a)]") //using index: ./td[2]
                        .InnerText;
}