简体   繁体   中英

Parsing html with html agility pack

I want to collect all tags in from this div but do not know how to do this in the best way with xpath method

<div class="biz_info">
    <h3><a href="/profil/78122/s%C3%B8rby-rehab/">Sørby Rehab</a></h3>
    <table class="string_14">
        <tbody>
            <tr>
               <td>Postadr.:</td> 
               <td class="tab_space">Rognerudveien 8 B, 0681 Oslo</td> 
            </tr>

            <tr>
                <td>Telefon:</td> 
                <td class="tab_space">928 70 700</td>
            </tr>

            <tr>
                <td>Nettside:</td> 
                <td class="tab_space"><a href="http://www.sorby-rehab.no" target="_blank">www.sorby-rehab.no</a></td>
            </tr>
        </tbody>
    </table>
</div>

Today my code looks like this (but very bad):

 HtmlDocument doc = new HtmlDocument();
doc.Load(new StringReader(result));
HtmlNode root = doc.DocumentNode;

List<string> anchorTags = new List<string>();

foreach (HtmlNode link in root.SelectNodes("//@class=biz_info"))
{
    string att = link.OuterHtml;
    anchorTags.Add(att);
}

Is someone who is professional in xpath that can help me?

HtmlDocument html = new HtmlDocument();
html.Load(new StringReader(result));
var anchorTags = html.DocumentNode.SelectNodes("//div[@class='biz_info']//a")
                     .Select(a => a.OuterHtml)
                     .ToList();

That will give you list of anchor tags html. If you need just urls:

urls = html.DocumentNode.SelectNodes("//div[@class='biz_info']//a[@href!='']")
           .Select(a => a.Attributes["href"].Value)
           .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM