4

嘿,我正在尝试通过执行以下操作从网页中获取页面标题和 H1 标签

    doc.LoadHtml(htmlSourceCode)

    txtTitle.Text = doc.GetElementsByTagName("title").InnerText()

    txtH1.Text = doc.GetElementsByTagName("H1").InnerText()

    For Each channel In doc.DocumentNode.SelectNodes(".//meta[@name='description']")
        txtDescription.Text = channel.Attributes("content").Value
    Next

上面唯一有效的代码是 txtDescription 部分。标题和 H1 都没有。为了获得这两个标签,我需要使用什么类型的语法?

html 代码如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>
    The title text is here!
</title><link rel="icon" type="image/x-icon" href="http://www.zzzz.com/favicon.ico" />
....
<div class="main-content">
    <div class="block-info">
        <div class="container">
            <div class="article">
                <h1>
                    This is the H1 tag with the text!</h1>

<p>As the 2nd held tru
4

1 回答 1

10

您可以使用doc.DocumentNode.SelectSingleNode("//head/title")doc.DocumentNode.SelectNodes("//body//h1")

doc.DocumentNode.Descendants("title").SingleOrDefault()doc.DocumentNode.Descendants("h1")

于 2013-01-14T14:38:07.367 回答