Ruby 我如何使用Nokogiri到达这个节点?

Ruby 我如何使用Nokogiri到达这个节点?,ruby,nokogiri,Ruby,Nokogiri,以下是我的html的开始: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-mic

以下是我的html的开始:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<!--[if !mso]><style>v\\:* {behavior:url(#default#VML);}\no\\:*  {behavior:url(#default#VML);}\nw\\:* {behavior:url(#default#VML);}\n.shape {behavior:url(#default#VML);}\n</style><![endif]--><style><!--\n/* Font Definitions */\n@font-face\n\t{font-family:"Cambria Math";\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Calibri;\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\n@font-face\n\t{font-family:Tahoma;\n\tpanose-1:2 11 6 4 3 5 4 4 2 4;}\n/* Style Definitions */\np.MsoNormal, li.MsoNormal, div.MsoNormal\n\t{margin:0in;\n\tmargin-bottom:.0001pt;\n\tfont-size:12.0pt;\n\tfont-family:"Times New Roman","serif";}\na:link, span.MsoHyperlink\n\t{mso-style-priority:99;\n\tcolor:blue;\n\ttext-decoration:underline;}\na:visited, span.MsoHyperlinkFollowed\n\t{mso-style-priority:99;\n\tcolor:purple;\n\ttext-decoration:underline;}\np\n\t{mso-style-priority:99;\n\tmso-margin-top-alt:auto;\n\tmargin-right:0in;\n\tmso-margin-bottom-alt:auto;\n\tmargin-left:0in;\n\tfont-size:12.0pt;\n\tfont-family:"Times New Roman","serif";}\nspan.EmailStyle18\n\t{mso-style-type:personal-reply;\n\tfont-family:"Calibri","sans-serif";\n\tcolor:#1F497D;}\n.MsoChpDefault\n\t{mso-style-type:export-only;\n\tfont-size:10.0pt;}\n@page WordSection1\n\t{size:8.5in 11.0in;\n\tmargin:1.0in 1.0in 1.0in 1.0in;}\ndiv.WordSection1\n\t{page:WordSection1;}\n--> </style>
<!--[if gte mso 9]><xml>\n<o:shapedefaults v:ext="edit" spidmax="1026" />\n</xml><![endif]--> <!--[if gte mso 9]>    <xml>\n<o:shapelayoutv:ext="edit">\n<o:idmapv:ext="edit"data="1"/>\n</o:shapelayout></xml><![endif]-->

</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><p>&nbsp;</p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><p>&nbsp;</p></span></a></p>
<div><div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in"><p class="MsoNormal"><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> EMAIL SENDER NAME [mailto:EMAILADDRESS@FAKE.COM] <br><b>Sent:</b>!! DATE I NEED TO GRAB HERE !! <br><b>To:</b> EMAIL ADDRESS HERE <br><b>Subject:</b> SUBJECT LINE HERE <p></p></span></p></div></div>
我还在
标签\u标签\u名称
中尝试了一些较短的路径,基本上在开头添加了另一个HTML标签


不过,每次发送日期都会返回
nil

该文档中没有多少内容可供浏览。使用一个选择器可靠地找到最近的点,然后用正则表达式抓取文本:

> doc.css("div.WordSection1 p.MsoNormal span").text[/Sent:\n(.*)/, 1]
=> "          !! DATE I NEED TO GRAB HERE !! To:"

该文档中没有太多内容可供浏览。使用一个选择器可靠地找到最近的点,然后用正则表达式抓取文本:

> doc.css("div.WordSection1 p.MsoNormal span").text[/Sent:\n(.*)/, 1]
=> "          !! DATE I NEED TO GRAB HERE !! To:"

您感兴趣的源代码是(为了清晰起见,我删除了属性):

如果需要其他字段,可以使用
css
而不是
at_css

date_string = @doc.css('div div p>span b')[0].next
to_string = @doc.css('div div p>span b')[1].next
subject_string = @doc.css('div div p>span b')[2].next

我会留下来让你做些事情,让你知道发件人的名字

您感兴趣的源代码是(为了清晰起见,我删除了属性):

如果需要其他字段,可以使用
css
而不是
at_css

date_string = @doc.css('div div p>span b')[0].next
to_string = @doc.css('div div p>span b')[1].next
subject_string = @doc.css('div div p>span b')[2].next
我会留下来让你做些事情,让你知道发件人的名字

我先从这个开始:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
    <title></title>
</head>

<body>
    <div class="WordSection1">

        <div>
            <div>
                <b>Sent:</b>!! DATE I NEED TO GRAB HERE !!<br>
                <b>To:</b> EMAIL ADDRESS HERE<br>
                <b>Subject:</b> SUBJECT LINE HERE</span></p>
            </div>
        </div>
    </div>
</body>
</html>
EOT

text = doc.at('div.WordSection1').text
sent_date = text[/Sent:(.+)To:/, 1].strip
puts sent_date
示例HTML非常混乱,因此您无法轻松看到您想要在该林中看到的特定树。去掉所有对导航不重要的东西,然后构建搜索

而且,虽然解析器是一个很好的工具,但有时使用它来获取所需的文本,然后通过字符串搜索获取特定内容更容易。

我从以下内容开始:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
    <title></title>
</head>

<body>
    <div class="WordSection1">

        <div>
            <div>
                <b>Sent:</b>!! DATE I NEED TO GRAB HERE !!<br>
                <b>To:</b> EMAIL ADDRESS HERE<br>
                <b>Subject:</b> SUBJECT LINE HERE</span></p>
            </div>
        </div>
    </div>
</body>
</html>
EOT

text = doc.at('div.WordSection1').text
sent_date = text[/Sent:(.+)To:/, 1].strip
puts sent_date
示例HTML非常混乱,因此您无法轻松看到您想要在该林中看到的特定树。去掉所有对导航不重要的东西,然后构建搜索


而且,虽然解析器是一个很好的工具,但有时使用它来获取所需的文本,然后通过字符串搜索获取特定内容更容易。

添加示例数据时,请将其剥离到作为示例所需的最少量。当你添加样本数据时,请将其剥离到作为示例所需的最少量。如果你做了更多的事情,你就浪费了那些回答的人的时间,因为他们在费力地处理那些不必要的事情。
!! DATE I NEED TO GRAB HERE !!