Html Agility Pack SelectSingleNode giving always same result in iteration?

旧巷老猫 提交于 2019-12-04 17:00:15

问题


I would like the nodes in the collection but with iterating SelectSingleNode I keep getting the same object just node.Id is changing... What i try is to readout the webresponse of a given site and catch some information like values, links .. in special defined elements.

int offSet = 0;
string address = "http://www.testsite.de/ergebnisliste.html?offset=" + offSet;

HtmlWeb web = new HtmlWeb();
//web.OverrideEncoding = Encoding.UTF8;
HtmlDocument doc = web.Load(address);

HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//div[@itemtype='http://schema.org/Posting']");

foreach (HtmlNode node in collection) {
    string id = HttpUtility.HtmlDecode(node.Id);
    string cpname = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='name']").InnerText);
    string cptitle = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='title']").InnerText);
    string cpaddress = HttpUtility.HtmlDecode(node.SelectSingleNode("//span[@itemprop='addressLocality']").InnerText);
    string date = HttpUtility.HtmlDecode(node.SelectSingleNode("//div[@itemprop='datePosted']").InnerText);
    string link = "http://www.testsite.de" + HttpUtility.HtmlDecode(node.SelectSingleNode("//div[@class='h3 title']//a[@href]").GetAttributeValue("href", "default"));               
}

This is for example for 1 iteration:

<div id="66666" itemtype="http://schema.org/Posting">   
<div>
    <a>
        <img />
    </a>
</div>
<div>
    <div class="h3 title">
        <a href="/test.html"  title="Test">
            <span itemprop="title">Test</span>
        </a>
    </div>
    <div>
        <span itemprop="name">TestName</span>       
    </div>
</div>
<div>
    <div>
        <div>
            <div>
                <span itemprop="address">Test</span>
            </div>
            <span>                     
                <a>
                    <span><!-- --></span>
                    <span></span>
                </a>
            </span>         
        </div>
    </div>      
    <div itemprop="date">
        <time datetime="2013-03-01">01.03.13</time>
    </div>
</div>


回答1:


By writing

node.SelectSingleNode("//span[@itemprop='name']").InnerText

it's like you writing

doc.DocumentNode.SelectSingleNode("//span[@itemprop='name']").InnerText

To do what you want to do you should write it like this: node.SelectSingleNode(".//span[@itemprop='name']").InnerText.

This .dot / period tells make a search on the current node which is node instead on doc



来源:https://stackoverflow.com/questions/15185404/html-agility-pack-selectsinglenode-giving-always-same-result-in-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!