首页 文章

HtmlAgilityPack HtmlNode如何获取<select>和<option>标签

提问于
浏览
0

我正在使用HtmlAgilityPack HtmlNode,我想得到

<select> and <option> and all other html tags.

目前HtmlNode只解析html,table,div,...标签 . 我怎样才能获得选项并选择标签 .

我目前的代码:

if (htmlContent != null)
{
    doc.LoadHtml(htmlContent);

    if (doc.DocumentNode.SelectNodes("//*") == null)
    {
        List<TagValuePair> tempList = new List<TagValuePair>();
        tempList.Add(new TagValuePair("Error!", htmlContent, -1));
        return tempList;
    }

    foreach (HtmlNode tag in doc.DocumentNode.SelectNodes("//*"))
    {
        try
        {
            if (!string.IsNullOrEmpty(tag.InnerHtml))
            {
                if (!tagAppearance.Keys.Contains(tag.Name))
                {
                    tagAppearance.Add(tag.Name, 1);
                }
                else
                    tagAppearance[tag.Name] = tagAppearance[tag.Name] + 1;

                tagsValues.Add(
                    new TagValuePair(tag.Name, tag.InnerHtml.Trim(),
                    tagAppearance[tag.Name])
                );
            }
        }
        catch
        {
            return null;
        }
    }
}

我尝试着

doc.DocumentNode.SelectNodes("//option");

我得到选项标签,但没有其他人 . 如何获得所有选项并选择

编辑:

需要从这个html中获取选项中的所有数据:

<select onchange="javascript:submitGlobalDiscountSvetisce(this.value);submitGlobalDiscount(this.value);" name="datumper">
                  <option value="18.6.2011|7">18.jun. 7 noči od 515,00&nbsp;EUR</option>
<option value="25.6.2011|7">25.jun. 7 noči od 515,00&nbsp;EUR</option>
<option value="2.7.2011|7">2.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="9.7.2011|7">9.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="16.7.2011|7">16.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="23.7.2011|7">23.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="30.7.2011|7">30.jul. 7 noči od 529,00&nbsp;EUR</option>
<option value="6.8.2011|7">6.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="13.8.2011|7">13.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="20.8.2011|7">20.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="3.9.2011|7">3.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="10.9.2011|7">10.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="17.9.2011|7">17.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="24.9.2011|7">24.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="1.10.2011|7">1.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="8.10.2011|7">8.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="15.10.2011|7">15.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="22.10.2011|7">22.okt. 7 noči od 567,00&nbsp;EUR</option>
<option value="29.10.2011|7">29.okt. 7 noči od 567,00&nbsp;EUR</option>

                </select>

2 回答

  • 3

    你可以使用 html.DocumentNode.SelectSingleNode("//select[@id='idElement']").OuterHtml 这一行带来选择和选项 .

    快乐守则

  • 0

    默认情况下,OPTION标记会被特别处理(出于历史原因) . 在SO上看到我在这个帖子上的答案:Parsing HTML Reading Option Tag Content with HtmlAgillityPack

相关问题