【问题标题】:C# HtmlAgilityPack Xpath problems, trouble finding H4 innertextC# HtmlAgilityPack Xpath 问题,找不到 H4 内文
【发布时间】:2014-05-19 04:13:18
【问题描述】:

我有一种方法可以在网页的某个部分中找到我要查找的所有内容,但我在尝试在节点中查找 H4 时遇到了困难。 //div[@class='job'] 的 xpath 正确找到了我正在寻找的所有 8 个事件。但是在我尝试遍历这 8 次事件后,我遇到了问题。

这是我正在查看的代码的 HTML 输出。

<div class="job_art ">
<div style="background: #444      url('https://a.akamaihd.net/mwfb/mwfb/graphics/jobs/chicago/meet_with_the_south_gang_family_    760x225_01.jpg') 50% 0 no-repeat;">
</div>
</div>
<div class="job_details clearfix">
<h4>Meet With the South Gang Family</h4>
<div class="mastery_bar" title="Indicates how much of this Job you&#39;ve mastered.      Master Jobs to earn Skill Points."><div style="width: 0%" class="noHighlight"></div><p>100%     Mastered</p><div style="width: 0%"><p>100% Mastered</p></div></div><ul class="uses clearfix"     style="width:100px;"><li class="energy" base_value="2" current_value="2" title="Spend 2     Energy to do this Job once.">2</li></ul><ul class="pays clearfix" style="width:120px"     title="Earn XP, City Cash and Loot items while doing Jobs."><li class="experience" base_value="2" current_value="2">2</li><li class="cash_icon_jobs_8" base_value="2" current_value="2">2</li></ul><a id='btn_dojob_1' class='sexy_button_new sexy_energy_new medium orange impulse_buy' selector='#inner_page' requirements='{"energy":2}' precall='BrazilJobs.preDoJob' callback='BrazilJobs.doJob' href='remote/h.php?job=1&tab=1&clkdiv=btn_dojob_1'><span><span>Do Job</span></span></a></div><div class="job_additional_results"><div id="loot-bandit-1" class="lootContainer"></div><div class="previous_loot"></div></div><div id="bandit-contextual-1" class="contextual bandit-contextual"></div>

它总是会找到像“Clams(Bank)”这样的东西,我不知道怎么找到的。问题开始于

  string MissionName = node.SelectSingleNode("//h4").InnerText;

我尝试了很多 xpath,例如 //div[h4[1]]、h4[1]。我只需要第一次出现,因为它只出现一次。问题从我的代码哪里开始?

我需要内文《见见南帮家族》

public static List<string> GetMissions()
    {
        List<string> FoundMissions = new List<string>();

        HTML_CONTENT = HTML_CONTENT.Replace("\r", "");
        HTML_CONTENT = HTML_CONTENT.Replace("\t", "");
        HTML_CONTENT = HTML_CONTENT.Replace("\n", "");
        HTML_CONTENT = HTML_CONTENT.Replace("\\", "");

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.Load(new StringReader(HTML_CONTENT));

        if(doc.DocumentNode == null)
            return FoundMissions;
        var DivNodes = doc.DocumentNode.SelectNodes("//div[@class='job ']");
        if (DivNodes != null)
        {
            string Count = DivNodes.Count.ToString();

就像我说的,它发现所有 8 次出现都很好。我调试并得到了上面的 HTML,我把它放在了上面,所以我认为这部分很好。

            foreach (HtmlNode node in DivNodes)
            {

                string MissionName = node.SelectSingleNode("//h4").InnerText;
            }
        }

        return FoundMissions;
        }


    }

【问题讨论】:

  • 或者你可以试试 node.selectSingleNode("//div[@class='job_details clearfix']").InnerText;

标签: c# html dom xpath


【解决方案1】:

您需要通过在开头添加单点 (.) 来明确告知 XPath 查询与当前 node 相关:

string MissionName = node.SelectSingleNode(".//h4").InnerText;

否则,XPath 将从根节点开始搜索。这可能是导致您的尝试得到错误结果的原因。

【讨论】:

  • 在我的生活中,从未有过如此重要的时期。 :) 我很惊讶我的代码是 99% 正确的。
猜你喜欢
  • 2013-10-15
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多