使用 Nokogiri 为特定的 <td> 标签解析 HTML

Parsing HTML for specific <td> tags with Nokogiri

我目前正在开发 SSL 证书枚举工具,该工具将查询 https://crt.sh 特定网站,并抓取结果以查找子域。我正在使用 Mechanize 将结果页面获取为 HTML,我需要解析某些特定 table 数据的响应。以下是一行结果的示例

<tr>
  <td style="text-align:center"><a href="?id=47689622">47689622</a></td>
  <td style="text-align:center">2016-10-22</td>
  <td style="text-align:center">2016-05-21</td>
  <td>*.meta.whosebug.com</td>
  <td><a style="white-space:normal" href="?caid=1397">C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert SHA2 High Assurance Server CA</a></td>
</tr>

我需要一种方法来只提取倒数第​​二个标签,它显然没有附加 id 或 class。有没有人有类似的经验?如果是这样,任何提示将不胜感激。我从控制器获取文件的方式如下。

domain = params[:domain_name]
@result = "Retrieving domain information from crt.sh\nSee https://crt.sh/?q=%25#{domain} to validate manually\n\n"
host = ENV["https_proxy"][8..-1].split(":")[0]    
port = ENV["https_proxy"].split(":")[2].chomp("/")

agent = Mechanize.new
agent.user_agent = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)'
agent.set_proxy(host, port)
html_doc = Nokogiri::HTML(agent.get("https://crt.sh/?q=%25#{domain}").body, 'UTF-8')

我对 Nokogiri 的经验不多,因为我一个月前才在 Rails 开始学习 Ruby,直到今天早些时候才需要 Nokogiri。

一旦你 select table 你就可以

table.last_element_child.previous

哪个 returns 最后一个 child 然后得到最后一个 child 的前一个兄弟。

https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet