Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:57:21+00:00 2026-05-20T10:57:21+00:00

I am doing one the examples at the mechanize doc site and I want

  • 0

I am doing one the examples at the mechanize doc site and I want to parse the results using
nokogiri.

My problem is that when the following line gets executed:

doc = Nokogiri::HTML(search_results, 'UTF-8' )

the following error occurs:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.4.1-x86-mingw32/lib/nokogiri/html/document.rb:71:in `parse': undefined method `name' for "UTF-8":String (NoMethodError)
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.4.1-x86-mingw32/lib/nokogiri/html.rb:13:in `HTML'
    from mechanize_test.rb:16:in `<main>'

I have installed ruby 1.9 on a windows vista machine

The results returned by mechanize are non-latin (utf8)

The code sample follows.

# encoding: UTF-8

 require 'rubygems'
 require 'mechanize'
 require 'nokogiri'

 agent = Mechanize.new
 agent.user_agent_alias = 'Mac Safari'
 page = agent.get("http://www.google.com/")
 search_form = page.form_with(:name => "f")
 search_form.field_with(:name => "q").value = "invitations"
 search_results = agent.submit(search_form)
 puts search_results.body

 doc = Nokogiri::HTML(search_results, 'UTF-8')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:57:21+00:00Added an answer on May 20, 2026 at 10:57 am

    This appears to be issue with what Nokogiri expects as parameters to the parse method that is being called. The first issue I see, is that you are passing in the encoding option in the wrong parameter slot,

    A parsing example from Nokogiri project page that specifies encoding

    Nokogiri.XML('<foo><bar /><foo>', nil, 'EUC-JP')
    

    Notice the encoding is the third parameter, not the second. But that still does not fully explain the behavior you are seeing, as the encoding should simply be ignored.

    Per the Nokogiri documentation a call to Nokogiri::HTML() is a convenience method for the parse method.

    Code for Nokogiri::HTML::parse

       def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
          document.parse(thing, url, encoding, options, &block)
       end
    

    The source for the Nokogiri::HTML::Document parse method is a bit long, but here is the relevant part though:

     string_or_io.respond_to?(:encoding)
       unless string_or_io.encoding.name == "ASCII-8BIT"
          encoding ||= string_or_io.encoding.name
       end
     end
    

    Notice string_or_io.encoding.name, this matches the error your saw, undefined method ‘name’ for “UTF-8”:String (NoMethodError).

    Does your search_results object has an attribute with a key value pair of {:encoding => ‘UTF-8’}? It appears Nokogiri is looking for the encoding to store an object that then has a name attribute of ‘UTF-8’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the examples I see for doing a query show only using one key.
I am doing one project on Struts 2 that I am getting knowledge little
I have seen quite a few examples/tutorials on the web for doing one-one shared
I'm doing a PHP site which displays code examples in various languages (C#, PHP,
I'm doing a shake detection in my app and I saw several examples using
I am doing some server side form validation and in the case that one
The Mongoid documentation only gives one example of doing a wildcard search: Person.where(first_name: /^d/i)
Iam doing one application in iphone.In this i need to get the browsing history
I'm doing one research on video encoding tools for flv. I tested flvtool2 and
i am doing one small application , wheich have login functionality, in the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.