问题查询网页解析与HTML敏捷包

本文关键字:HTML 查询 查询网 网页 问题 | 更新日期: 2023-09-27 18:07:15

我有以下源代码片段:

<div class = "discount_tools_row">
  <div class = "discount_tools">
    <ul> 
      <li><a href = "#" class = "share-discount" rel = "nofollow"></a></li>
      <li><a href = "/deal/map/4243683"
             class = "show-location"
             title = "הראה מקום על מפה"
             data-address = "רח&#39; האצ&quot;ל 39, ראשון לציון"></a></li>
    </ul>
    <link rel = "prerender"
          href = "http:/ / www.bigdeal.co.il / ? CampaignId = 873 & sId = 10 ">
    <a class = "tavo_button"
       data-provider = "bigdeal"
       href = "http : //www.bigdeal.co.il/?CampaignId=873&sId=10"
       target="_blank"
       rel = "nofollow">תבוא!</a>
    </div>
  </div>
</div>

使用HTML敏捷包我想获取对的<data-address value, link rel="prerender" href value>

我尝试了以下操作,但得到了错误的结果:

var nodes = doc.DocumentNode.SelectNodes(
    "//div[@class='"discount_tools'"]");
var geoNodes = nodes.Where(node => !string.IsNullOrEmpty(
    node.ChildAttributes("data-address").ToString()));
AnswerFormat ans = new AnswerFormat {
    Locations = geoNodes.Select(
        node => node.ChildAttributes("data-address").ToString()).ToList(),
    //Names = nodes.Select(node => node.Attributes["data-address"].Value).
    //ToList(),
    Details = geoNodes.Select(
        node => node.ChildAttributes("data-direct-url").ToString()).ToList()
};

我试图达到所有

< div class = "discount_tools" >

data-address

属性在其childNode和

  <a class="tavo_button" data-provider="bigdeal" href=

在另一个childNode

问题查询网页解析与HTML敏捷包

这就是我的解决方案:

        var nodes = doc.DocumentNode.SelectNodes("//div[@class='"discount_tools'"]");
        var linksCollections = nodes.Select(node => node.Descendants("a"));
        List<string> Locations = new List<string>();
        List<string> Categories = new List<string>();
        List<string> Hrefs = new List<string>();
        foreach (var col in linksCollections)
        {
            string location, category, href;
            location = GetAtt("data-address",col);
            if (!string.IsNullOrEmpty(location))
            {
                category = GetAtt("data-kind", col);
                if (!string.IsNullOrEmpty(category))
                {
                    href = GetAtt("data-provider", "href", col);
                    if (!string.IsNullOrEmpty(href))
                    {
                        Locations.Add(location);
                        Categories.Add(category);
                        Hrefs.Add(href);
                    }
                }
            }
        }
String dataAddressValue = doc.DocumentNode.SelectSingleNode("//div[@class='discount_tools']/ul/li/a[@class='show-location']").Attributes["data-address"].Value;
String LinkHrefValue = doc.DocumentNode.SelectSingleNode("//div[@class='discount_tools ']/link[@rel=’prerender’]").Attributes["href"].Value;