【问题标题】:Ruby webscrape script for GoDaddyGoDaddy 的 Ruby 网页抓取脚本
【发布时间】:2012-08-10 21:40:35
【问题描述】:

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

在抓取链接时遇到问题,然后我需要点击链接。我需要从下面的“GoToSecondaryDNS”js 元素中获取链接。我正在使用 Mechanize 和 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>

如何使用 Ruby 抓取链接“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

【问题讨论】:

    标签: ruby web-scraping nokogiri mechanize-ruby


    【解决方案1】:

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

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

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

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

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

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

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

    剩下的,比如如何访问 Mechanize 的内部 Nokogiri 文档,以及如何创建新的 URL,就留给你自己去想了。

    【讨论】:

    • 感谢您让我朝着正确的方向前进。我会看看我是否至少可以先获得返回的链接并更新这个线程。
    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    相关资源
    最近更新 更多