2

我正在 nessus-xmlrpc gem 中构建一个功能,以允许下载 HTML 报告。我只需要解析文件名值的 HTML 响应。

这是我正在使用的示例响应,来自 nessus:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Formatting the report</title><meta http-equiv="refresh"
content="5;url=/file/xslt/download/?fileName=Windows_-_Main___Media_kr1sjb.html">
</head>
<body bgcolor="#2b4e67">
<link type="text/css" href="jqueryui18.css" rel="stylesheet" />
<script type="text/javascript" src="jqueryui18.js"></script>
<div id="main"></div>

使用 Nokogiri 时:

doc = Nokogiri::HTML(http_content)

该文档最终如下:

#<Nokogiri::HTML::Document:0x4758128 name="document" children=[#<Nokogiri::XML::DTD:0x4757f48 name="html">, #<Nokogiri::XML::Element:0x47578ea name="html" children=[#<Nokogiri::XML::Element:0x47577aa name="head" children=[#<Nokogiri::XML::Element:0x475767e name="meta" attributes=[#<Nokogiri::XML::Attr:0x475764c name="charset" value="utf-8">]>, #<Nokogiri::XML::Element:0x4757368 name="title" children=[#<Nokogiri::XML::Text:0x475723c "Formatting the report">]>, #<Nokogiri::XML::Element:0x475af9a name="meta" attributes=[#<Nokogiri::XML::Attr:0x475ac48 name="http-equiv" value="refresh">, #<Nokogiri::XML::Attr:0x475ac2a name="content" value="5;url=/file/xslt/download/?fileName=Windows_-_Main___Media_kr1sjb.html">]>]>, #<Nokogiri::XML::Element:0x470b968 name="body" attributes=[#<Nokogiri::XML::Attr:0x470b7ec name="bgcolor" value="#2b4e67">] children=[#<Nokogiri::XML::Element:0x46fb216 name="link" attributes=[#<Nokogiri::XML::Attr:0x46fb18a name="type" value="text/css">, #<Nokogiri::XML::Attr:0x46fb176 name="href" value="jqueryui18.css">, #<Nokogiri::XML::Attr:0x46fb16c name="rel" value="stylesheet">]>, #<Nokogiri::XML::Element:0x46fa1d6 name="script" attributes=[#<Nokogiri::XML::Attr:0x46fa0e6 name="type" value="text/javascript">, #<Nokogiri::XML::Attr:0x46fa0c8 name="src" value="jqueryui18.js">]>, #<Nokogiri::XML::Element:0x46dd43c name="div" attributes=[#<Nokogiri::XML::Attr:0x46dd37e name="id" value="main">]>]>]>]>

我不知道如何取回fileName值,“Windows_- Main __Media_kr1sjb.html”。

任何帮助都会很棒,一旦它起作用,我将推动这些改变。

4

1 回答 1

1

我会做如下:

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eol
<html>
<head>
<meta charset="utf-8">
<title>Formatting the report</title><meta http-equiv="refresh"
content="5;url=/file/xslt/download/?fileName=Windows_-_Main___Media_kr1sjb.html">
</head>
<body bgcolor="#2b4e67">
<link type="text/css" href="jqueryui18.css" rel="stylesheet" />
<script type="text/javascript" src="jqueryui18.js"></script>
<div id="main"></div>
eol

str = doc.at_css('meta[http-equiv="refresh"]')['content']
# => "5;url=/file/xslt/download/?fileName=Windows_-_Main___Media_kr1sjb.html"
str[/\?fileName=(.*)/,1]
# => "Windows_-_Main___Media_kr1sjb.html"
于 2013-10-29T19:38:26.310 回答