0

我是 Ruby 新手,在我的第一个脚本任务中,我被要求编写一个网络抓取脚本来从 GoDaddy 抓取我们的 DNS 列表的元素。

在抓取链接时遇到问题,然后我需要关注链接。我需要从下面的“GoToSecondaryDNS”js 元素中获取链接。我正在使用机械化和 Nokogiri:

<td class="listCellBorder" align="left" style="width:170px;">
          <div style="padding-left:4px;">
            <div id="gvZones21divDynamicDNS"></div>
            <div id="gvZones21divMasterSlave" cicode="41022" onclick="GoToSecondaryDNS('iwanttoscrapethislink.com',0)" class="listFeatureButton secondaryDNSNoPremium" onmouseover="ShowSecondaryDNSAd(this, event);" onmouseout="HideAdInList(event);"></div>
            <div id="gvZones21divDNSSec" cicode="41023" class="listFeatureButton DNSSECButtonNoPremium" onmouseover="ShowDNSSecAd(this, event);" onmouseout="HideAdInList(event);" onclick="UpgradeLinkActionByID('gvZones21divDNSSec'); return false;" useClick="true" clickObj="aDNSSecUpgradeClicker"></div>
            <div id="gvZones21divVanityNS" onclick="GoToVanityNS('iwanttoscrapethislink.com',0)" class="listFeatureButton vanityNameserversNoPremium" onmouseover="ShowVanityNSAd(this, event);" onmouseout="HideAdInList(event);"></div>
            <div style="clear:both;"></div>
          </div>
        </td>

如何抓取链接“iwanttoscrapethislink.com”,然后与 onclick 交互以跟随链接并使用 Ruby 抓取下一页上的内容?

到目前为止,我对代码有一个简单的开始:

require 'rubygems'
require 'mechanize'
require 'open-uri'




def get_godaddy_data(url)


      web_agent = Mechanize.new

      result = nil

      ### login to GoDaddy admin


      page = web_agent.get('https://dns.godaddy.com/Default.aspx?sa=')

      ## there is only one form and it is the first form on thepage
      form = page.forms.first
      form.username = 'blank'
      form.password = 'blank'

      ## form.submit
      web_agent.submit(form, form.buttons.first)

     site_name = page.css('div.gvZones21divMasterSlave onclick td')  
      ### export dns zone data

      page = web_agent.get('https://dns.godaddy.com/ZoneFile.aspx?zone=' + site_name + '&zoneType=0&refer=dcc')
      form = page.forms[3]
      web_agent.submit(form, form.buttons.first).save(uri.host + 'scrape.txt')

       ## end

    end 

    ### read export file
    ##return File.open(uri.host + 'scrape.txt', 'rb') { |file| file.read }
  end


  def scrape_dns(url)

  site_name = page.css('div.gvZones21divMasterSlave onclick td') 
  LIST_URL = "https://dns.godaddy.com/ZoneFile.aspx?zone=" + site_name + '&zoneType=0&refer=dcc"
  page = Nokogiri::HTML(open(LIST_URL))

#not sure how to scrape onclick urls and then how to click through to continue scraping on the second page for each individual DNS

end
4

1 回答 1

1

您不能与“ onclick”交互,因为 Nokogiri 不是 JavaScript 引擎。

您可以提取内容,然后将其用作后续 Web 请求的 URL。假设doc包含解析的 HTML:

doc.at('div[onclick^="GoToSecondaryDNS"]')['onclick']

将为您提供onclick参数的值。^=意思是“找到以开头的单词”,这样我们就可以排除其他<div>带有参数的标签onclick并返回:

"GoToSecondaryDNS('iwanttoscrapethislink.com',0)"

使用简单的正则表达式[/'(.+)'/,1]将为您提供主机名:

doc.at('div[onclick^="GoToSecondaryDNS"]')['onclick'][/'(.+)'/,1]
=> "iwanttoscrapethislink.com"

其余的,比如如何访问 Mechanize 的内部 Nokogiri 文档,以及如何创建新的 URL,都留给你自己去解决。

于 2012-08-13T18:44:43.267 回答