c#htmlagilitypack强大而且拉扯

时间:2017-03-21 08:13:11

标签: c# html-agility-pack

enter image description here

我想拉出图片中的区域 Iam拉了2和3想

Uri url = new Uri("http://www.milliyet.com.tr/sondakika/");

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;

var html = client.DownloadString(url);
HtmlAgilityPack.HtmlDocument dokuman = new HtmlAgilityPack.HtmlDocument();
dokuman.LoadHtml(html);

HtmlNodeCollection basliklar = dokuman.DocumentNode.SelectNodes("//div[contains(@class,'kategoriList3')]//a");


foreach (var baslik in basliklar)
{
    try
    {
        datacıktı.Rows.Add();
        datacıktı.Rows[sayac].Cells[0].Value = baslik.Attributes["href"].Value.ToString();
        datacıktı.Rows[sayac].Cells[1].Value = baslik.InnerText;
        sayac++;
    }
    catch
    {
        continue;
    }
}

1 个答案:

答案 0 :(得分:0)

此代码可以帮助您

Uri url = new Uri("http://www.milliyet.com.tr/sondakika/");

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;

var html = client.DownloadString(url);
HtmlAgilityPack.HtmlDocument dokuman = new HtmlAgilityPack.HtmlDocument();
dokuman.LoadHtml(html);

IEnumerable<HtmlNode> htmlNodes = dokuman.DocumentNode.Descendants("ul").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("sonDK"));

foreach (HtmlNode htmlNode in htmlNodes)
{
      IEnumerable<HtmlNode> liList = htmlNode.Descendants("li").Where(l => (l.Attributes.Contains("class") && l.Attributes["class"].Value.Contains("title")) == false);

      foreach (HtmlNode liNode in liList)
      {
          Console.WriteLine("strong:" + liNode.FirstChild.InnerText + "- link:" + liNode.LastChild.Attributes["href"].Value);
      }
}
相关问题