【问题标题】:Parsing HTML for specific <td> tags with Nokogiri使用 Nokogiri 解析特定 <td> 标记的 HTML
【发布时间】:2017-02-07 19:42:48
【问题描述】:

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

<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.stackoverflow.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 或类。有没有人有类似的经验?如果是这样,任何提示将不胜感激。我从控制器获取文件的方式如下。

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 方面没有太多经验,因为我一个月前才开始学习 Ruby on Rails,直到今天早些时候才需要 Nokogiri。

【问题讨论】:

  • 那么,你想从&lt;a&gt;标签中获取内容吗?
  • @mr_sudaca 不,我正在尝试获取 *.meta.stackoverflow.com

标签: ruby-on-rails ruby nokogiri


【解决方案1】:

一旦你选择了你可以做的表

table.last_element_child.previous

返回最后一个孩子,然后获取最后一个孩子的前一个兄弟。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多