从 Scrubyt 过渡到 Nokogiri - 写入 XML 还是哈希?

发布于 2024-08-02 10:33:43 字数 935 浏览 8 评论 0原文

我正在尝试将这段代码从 scrapyt 转换为 nokogiri,但我一直试图将结果写入哈希或 xml。在 scrapyt 中,它看起来如下所示:

require 'rubygems'
  require 'scrubyt'

  result_data = Scrubyt::Extractor.define do
    fetch "http://www.amazon.com/gp/offer-listing/0061673730"
    results "//div[@class='resultsset']" do
       item "//tbody/tr" do
          condition "//div[@class = 'Condition']"
          price "//span[@class = 'price']"
          shipping "//span[@class = 'price_shipping']"
       end
   end
  end
  @description = result_data.to_xml
  return @description
end

使用 nokogiri,我可以解析出我想要的信息,但似乎没有一种快速的方法来返回散列或 xml 文档中的项目。这就是我在 nokogiri 中所拥有的一切。

require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))

doc.css('div.condition, span.price, span.price_shipping ').each do |item|
puts item.content
end 

如何将项目信息返回到 xml 或散列?

I'm trying to transition this bit of code from scrubyt to nokogiri, and am stuck trying to write my results to either a hash or xml. In scrubyt it looks like the following:

require 'rubygems'
  require 'scrubyt'

  result_data = Scrubyt::Extractor.define do
    fetch "http://www.amazon.com/gp/offer-listing/0061673730"
    results "//div[@class='resultsset']" do
       item "//tbody/tr" do
          condition "//div[@class = 'Condition']"
          price "//span[@class = 'price']"
          shipping "//span[@class = 'price_shipping']"
       end
   end
  end
  @description = result_data.to_xml
  return @description
end

With nokogiri I can parse out the information I want, but there doesn't seem to be a quick way to return items in a hash or xml document. Here's all I have in nokogiri.

require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))

doc.css('div.condition, span.price, span.price_shipping ').each do |item|
puts item.content
end 

How would one return item information to either xml or a hash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千年*琉璃梦 2024-08-09 10:33:44

你可能想省略“=”
xml.价格 = p.内容

you may want to omit "=" in
xml.price = p.content

风筝有风,海豚有海 2024-08-09 10:33:43

您可以使用 Builder 构建 XML。

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.items {
      doc.css('div.condition, span.price, span.price_shipping').each do |o|
        xml.item_content = o
      end
    }
  }
end

puts builder.to_xml

You can use the Builder to build XML.

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.items {
      doc.css('div.condition, span.price, span.price_shipping').each do |o|
        xml.item_content = o
      end
    }
  }
end

puts builder.to_xml
策马西风 2024-08-09 10:33:43

想通了...

require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
doc.xpath('//tbody[@class="result"]').each do |res|
    xml.result {
    res.css('span.price').each do |p|
      xml.price = p.content
    end
    res.css('span.price_shipping').each do |s|
      xml.ship = s.content
    end
    }
    end
}
end
puts builder.to_xml

结果:

<?xml version="1.0"?>
<root>
  <result>
    <price=>$6.09</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$6.48</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.12</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.31</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.52</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.52</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.53</price=>
  </result>
  <result>
    <price=>$7.56</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.61</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.61</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$8.59</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$8.99</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.05</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.32</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.32</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.55</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.56</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.42</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.59</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.90</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$12.07</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$12.35</price=>
    <ship=>+ $3.99</ship=>
  </result>
</root>

Figured it out...

require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
doc.xpath('//tbody[@class="result"]').each do |res|
    xml.result {
    res.css('span.price').each do |p|
      xml.price = p.content
    end
    res.css('span.price_shipping').each do |s|
      xml.ship = s.content
    end
    }
    end
}
end
puts builder.to_xml

Results:

<?xml version="1.0"?>
<root>
  <result>
    <price=>$6.09</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$6.48</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.12</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.31</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.52</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.52</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.53</price=>
  </result>
  <result>
    <price=>$7.56</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.61</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.61</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$7.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$8.59</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$8.99</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.05</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.32</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.32</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.55</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$10.56</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.42</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.59</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.90</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$11.95</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$12.07</price=>
    <ship=>+ $3.99</ship=>
  </result>
  <result>
    <price=>$12.35</price=>
    <ship=>+ $3.99</ship=>
  </result>
</root>
玉环 2024-08-09 10:33:43

谢谢!这正是我所需要的。不过,我无法正确循环。

require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))
builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.item {
      doc.css('span.price').each do |o|
        xml.price = o
      doc.css('span.price_shipping').each do |o|

      end
      end
    }
  }
end

puts builder.to_xml

这将返回:

<?xml version="1.0"?>
<root>
  <item>
    <price=><span class="price">$6.09</span></price=>
    <price=><span class="price">$6.48</span></price=>
    <price=><span class="price">$11.95</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>

  </item>
</root>

我将如何重写我的代码以返回类似这样的内容:

<?xml version="1.0"?>
<root>
  <item>
    <price=><span class="price">$6.09</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>
  <item>
    <price=><span class="price">$6.48</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>
  <item>
    <price=><span class="price">$11.95</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>  
</root>

Thanks! That's exactly what I need. I'm having trouble looping correctly, though.

require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.amazon.com/gp/offer-listing/0061673730'))
builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.item {
      doc.css('span.price').each do |o|
        xml.price = o
      doc.css('span.price_shipping').each do |o|

      end
      end
    }
  }
end

puts builder.to_xml

That returns this:

<?xml version="1.0"?>
<root>
  <item>
    <price=><span class="price">$6.09</span></price=>
    <price=><span class="price">$6.48</span></price=>
    <price=><span class="price">$11.95</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>

  </item>
</root>

How would I rewrite my code to return something like this:

<?xml version="1.0"?>
<root>
  <item>
    <price=><span class="price">$6.09</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>
  <item>
    <price=><span class="price">$6.48</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>
  <item>
    <price=><span class="price">$11.95</span></price=>
    <ship=><span class="price_shipping">+ $3.99</span></ship=>
  </item>  
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文