nokogiri

Nokogiri gem installation error with spree

不羁的心 提交于 2019-12-05 15:02:08
I'm getting this error when I try to bundle install Bundler::GemspecError: Could not read gem at /home/theareba/.rvm/gems/ruby-2.0.0-p353/cache/nokogiri-1.6.1.gem. It may be corrupted. An error occurred while installing nokogiri (1.6.1), and Bundler cannot continue. Make sure that `gem install nokogiri -v '1.6.1'` succeeds before bundling. I've tried removing nokogiri in the cache and bundle installing again in vain. Here's my gemfile source 'https://rubygems.org' gem 'rails', '4.0.0' ruby "2.0.0" gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '2-1-stable' gem 'spree',

how to use nokogiri methods .xpath & .at_xpath

a 夏天 提交于 2019-12-05 13:16:26
I'm learning how to use nokogiri and few questions came to me based on the code below require 'rubygems' require 'mechanize' post_agent = WWW::Mechanize.new post_page = post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708') puts "\nabsolute path with tbody gives nil" puts post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]').xpath('text()').to_s.strip.inspect puts "\n.at_xpath gives an empty string" puts post_page.parser.at_xpath("//div[@id='posts']/div/table/tr/td/div[2]").at_xpath('text()').to_s.strip.inspect puts "\ntwo lines solution with .at

Why isn't Nokogiri installing?

帅比萌擦擦* 提交于 2019-12-05 13:14:07
I'm having trouble installing Nokogiri. When I run bundle install or gem install nokogiri the installation fails. The error I'm getting is: (Note: This failure is from using the installation command on nokogiri.org) Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension. /Users/roneesh/.rbenv/versions/1.9.3-p194/bin/ruby extconf.rb --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28/ --with-iconv

Convert HTML to plain text and maintain structure/formatting, with ruby

纵然是瞬间 提交于 2019-12-05 11:07:55
I'd like to convert html to plain text. I don't want to just strip the tags though, I'd like to intelligently retain as much formatting as possible. Inserting line breaks for <br> tags, detecting paragraphs and formatting them as such, etc. The input is pretty simple, usually well-formatted html (not entire documents, just a bunch of content, usually with no anchors or images). I could put together a couple regexs that get me 80% there but figured there might be some existing solutions with more intelligence. First, don't try to use regex for this. The odds are really good you'll come up with

How to add attribute to Nokogiri node?

☆樱花仙子☆ 提交于 2019-12-05 09:53:57
问题 I'm trying to add an attribute to an existing Nokogiri node. What I've done is this: node.attributes['foobar'] = Nokogiri::XML::Attr.new('foo', 'bar') But I get the error: TypeError Exception: wrong argument type String (expected Data) What is a Data data type, and how do I add an attribute to the Nokogiri object? Thanks! 回答1: I believe you should just need to use the []= method, i.e. node['foo'] = 'bar' You could also use node.set_attribute('foo', 'bar') . 来源: https://stackoverflow.com

Append elements using Nokogiri::XML::Builder

纵然是瞬间 提交于 2019-12-05 09:39:31
I have the following code: builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| xml.myRoot do |xml| xml.oneChild xml.anotherChild end end Now I want to append a few child nodes to myRoot using the builder (in a second step, I know how to append them straight away). How can I do that? I've tried this: node = builder.doc.xpath('//myRoot/oneChild').first Nokogiri::XML::Builder.with(node) do |xml| xml.childOfOneChild 'Im a child of oneChild' end Which doesn't work. They won't stick to the element, it's just an empty oneChild. Your code produces the following XML, which seems to meet

Using XPath on single node returns elements in all nodes

徘徊边缘 提交于 2019-12-05 08:24:50
问题 I am parsing an XML doc that looks something like this: <MyBook> <title>Favorite Poems</title> <issn>123-456</issn> <pages>45</pages> </MyBook> <MyBook> <title>Chocolate Desserts</title> <issn>654-098</issn> <pages>100</pages> </MyBook> <MyBook> <title>Jabberwocky</title> <issn>454-545</issn> <pages>19</pages> </MyBook> I use xpath to pull out the MyBook nodes and iterate through them like so: xmldoc.xpath("//MyBook").each do |node| mytitle=node.xpath("//title").text puts mytitle end the

How to scrape images from eBay and Amazon using XPath in Nokogiri from JSON

懵懂的女人 提交于 2019-12-05 07:29:06
I'm trying to scrape images from websites using Nokogiri and XPath, so far with limited success. For a typical website whose HTML has img and src , I can use: tmp2 = Nokogiri::HTML(open(site_url)) tmp2.xpath("//img/@src").each do |src| ...do whatever end However, some sites like Amazon and eBay only trigger certain images with JavaScript. If I look at the code I can see the data in arrays. For example, from Amazon : <script type="text/javascript"> P.when('jQuery', 'cf').execute(function($, cf){ P.load.js('http://z-ecx.images-amazon.com/images/G/01/browser-scripts/imageBlock-udp-airy/imageBlock

Require Nokogiri? No such file to load

夙愿已清 提交于 2019-12-05 07:09:29
I'm trying to get started with using Nokogiri. I ran the command gem install nokogiri as an administrator in Windows 7 (64-Bit). The console said "successfully installed" and "1 gem installed". When I type in gem list --local OR gem q --local I see Nokogiri on the list of "Local Gems". However, when I try to use it via the require statement (in NetBeans), I get an error that there is "no such file to load". What am I doing wrong? I'm not a Ruby professional. This is also the first gem I've installed. Please dumb it down for me. Netbeans comes with built-in jRuby. You can specify or check wich

Nokogiri equivalent of jQuery closest() method for finding first matching ancestor in tree

末鹿安然 提交于 2019-12-05 05:34:58
jQuery has a lovely if somewhat misnamed method called closest() that walks up the DOM tree looking for a matching element. For example, if I've got this HTML: <table src="foo"> <tr> <td>Yay</td> </tr> </table> Assuming element is set to <td> , then I can figure the value of src like this: element.closest('table')['src'] And that will cleanly return "undefined" if either of the table element or its src attribute are missing. Having gotten used to this in Javascriptland, I'd love to find something equivalent for Nokogiri in Rubyland, but the closest I've been able to come up with is this