用Nokogiri解析nessus HTTP响应?

时间:2013-10-29 19:24:17

标签: html ruby nokogiri

我正在为nessus-xmlrpc gem构建一个功能,以允许下载HTML报告。我只需要解析fileName值的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”。

任何帮助都会很精彩,一旦它正常工作,我会推动这些改变。

1 个答案:

答案 0 :(得分: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"