Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Nokogiri解析HTML字符串_Html_Ruby_Nokogiri - Fatal编程技术网

使用Nokogiri解析HTML字符串

使用Nokogiri解析HTML字符串,html,ruby,nokogiri,Html,Ruby,Nokogiri,我试图编写一个ruby脚本,解析HTML字符串并从特定节点获取一些值 目前,我正在努力将字符串读入Nokogiri文档: 此代码: #!/usr/bin/ruby html_doc = Nokogiri::HTML("<html> <meta content="text/html; charset=UTF-8"/> <body style='margin:20px'> <p>The following user has register

我试图编写一个ruby脚本,解析HTML字符串并从特定节点获取一些值

目前,我正在努力将字符串读入Nokogiri文档:

此代码:

#!/usr/bin/ruby

html_doc = Nokogiri::HTML("<html>  <meta content="text/html; charset=UTF-8"/>  <body style='margin:20px'>    <p>The following user has registered a device, click on the link below to review the user and make any changes if necessary.</p>    <ul style='list-style-type:none; margin:25px 15px;'>      <li><b>User name:</b> Test User</li>      <li><b>User email:</b> test@abc.com</li>      <li><b>Identifier:</b> abc123def132afd1213afas</li>      <li><b>Description:</b> Tom's iPad</li>      <li><b>Model:</b> iPad 3</li>      <li><b>Platform:</b> </li>      <li><b>App:</b> Test app name</li>      <li><b>UserID:</b> </li>     </ul>    <p>Review user: https://cirrus.app47.com/users?search=test@abc.com</p>            <hr style='height=2px; color:#aaa'/>        <p>We hope you enjoy the app store experience!</p>        <p style='font-size:18px; color:#999'>Powered by App47</p>      <img src='https://cirrus.app47.com/notifications/562506219ac25b1033000904/img' alt=''/></body></html>")
#/usr/bin/ruby
html_doc=Nokogiri::html("以下用户已注册设备,请单击下面的链接查看用户并在必要时进行任何更改。

  • 用户名:测试用户
  • 用户电子邮件:test@abc.com
  • 标识符:abc123def132afd1213afas
  • 说明:汤姆的iPad
  • 型号:iPad 3
  • 平台:
  • 应用程序:测试应用程序名称
  • 用户ID:
    • 查看用户:https://cirrus.app47.com/users?search=test@abc.com


      我们希望您享受app store体验!

      由App47供电)

产生以下错误:

$ ruby emailParser.rb 
emailParser.rb:3: syntax error, unexpected tIDENTIFIER, expecting ')'
...ML("<html>  <meta content="text/html; charset=UTF-8"/>  <bod...
...                               ^
emailParser.rb:3: syntax error, unexpected tSTRING_BEG, expecting end-of-input
...tent="text/html; charset=UTF-8"/>  <body style='margin:20px'...
...                               ^
$ruby emailParser.rb
emailParser.rb:3:语法错误,意外的tIDENTIFIER,应为“')”

…ML(您必须将html字符串引号从“改为”,并将内的字符串引号改为“html”。类似的操作应该可以:

#!/usr/bin/ruby

html_doc = Nokogiri::HTML('<html>  <meta content="text/html; charset=UTF-8"/>  <body style="margin:20px">    <p>The following user has registered a device, click on the link below to review the user and make any changes if necessary.</p>    <ul style="list-style-type:none; margin:25px 15px;">      <li><b>User name:</b> Test User</li>      <li><b>User email:</b> test@abc.com</li>      <li><b>Identifier:</b> abc123def132afd1213afas</li>      <li><b>Description:</b> Tom\'s iPad</li>      <li><b>Model:</b> iPad 3</li>      <li><b>Platform:</b> </li>      <li><b>App:</b> Test app name</li>      <li><b>UserID:</b> </li>     </ul>    <p>Review user: https://cirrus.app47.com/users?search=test@abc.com</p>            <hr style="height=2px; color:#aaa"/>        <p>We hope you enjoy the app store experience!</p>        <p style="font-size:18px; color:#999">Powered by App47</p>      <img src="https://cirrus.app47.com/notifications/562506219ac25b1033000904/img" alt=""/></body></html>')
!/usr/bin/ruby
html_doc=Nokogiri::html(“以下用户已注册设备,请单击下面的链接查看用户并在必要时进行任何更改。

  • 用户名:测试用户
  • 用户电子邮件:test@abc.com
  • 标识符:abc123def132afd1213afas
  • 说明:Tom的iPad型号:iPad 3平台:
  • 应用程序:测试应用程序名称
  • 用户ID:
  • 查看用户:https://cirrus.app47.com/users?search=test@abc.com


    我们希望您能享受AppStore体验!

    由App47提供支持


问题在于字符串中有双引号,这会使解析器感到困惑,因为您还使用双引号来包围字符串。为了说明:

将“foo”放在条上
#=>SyntaxError:意外的tIDENTIFIER,预期输入结束
#把“foo”放在“bar”
#                 ^
您可能想让它打印
foo“bar
,但是当解析器到达第二个
(在
foo
之后)时,它认为字符串结束了,因此它之后的内容会导致语法错误。(堆栈溢出的语法高亮显示甚至会提示您在第一行
“foo”上如何查看。)
的颜色与
栏“
的颜色不同?一个好的语法高亮文本编辑器也会做同样的事情。)

一种解决方案是使用单一报价:

将“bar”放在“baz”
#=>巴“巴
这解决了本例中的问题,但实际上对您没有帮助,因为字符串中还有单引号

另一种解决方案是通过在引号前面加上
\
,来避开引号,如下所示:

将“foo\”放在“bar”上
#=>foo“条
…但对于像您这样的长字符串来说,这有点乏味(有时很棘手)。更好的解决方案是使用一种称为“heredoc”的特殊类型的字符串(对于“here document”,就其价值而言):


str=立即在HTML内部使用单引号或在HTML外部使用单引号
text/HTML;charset=UTF-8
不是字符串的一部分