• "Car 1"
  • "Car 2"
  • "Car 3"
  • "Car 4"
    • "Car 1"
    • "Car 2"
    • "Car 3"
    • "Car 4"
      • "Car 1"
      • "Car 2"
      • "Car 3"
      • "Car 4"
      • TechQA.

        How to get text from list items with Mechanize?

        1.1k views Asked by user3131148 At 2015-06-18T03:02:56+00:00 18 June 2015 at 03:02 2025-12-24T08:26:26+00:00
        <div class="carstd">
          <ul>
            <li class="cars">"Car 1"</li>
            <li class="cars">"Car 2"</li>
            <li class="cars">"Car 3"</li>
            <li class="cars">"Car 4"</li>
          </ul>
        </div>
        

        I want strip the text from each list item with mechanize and print it out. I've tried puts page.at('.cars').text.strip but it only gets the first item. I've also tried

        page.links.each do |x|
          puts x.at('.cars').text.strip
        
        end 
        

        But I get an error undefined method 'at' for #<Mechanize::Page::Link:0x007fe7ea847810>.

        ruby rubygems mechanize mechanize-ruby
        Original Q&A
        1

        There are 1 answers

        0
        pguardiario pguardiario On 2015-06-18T23:16:36+00:00 18 June 2015 at 23:16

        There's no links there. Links are a elements that get converted into special Mechanize objects. You want something like:

        page.search('li.cars').text # the text of all the li's mashed together as a string
        

        or

        page.search('li.cars').map{|x| x.text} # the text of each `li` as an array of strings
        

        Related Questions in RUBY

        • how to integrate cashfree payment gateway in ruby on rails project
        • RSpec Capybara throwing Selenium error when trying to click a button with browser confirm
        • Duplicate GET requests - Rails & Heroku
        • convert csv file with json data inside to a column, rows table in 2nd csv file
        • Installing dependencies from a gemspec file
        • Verifying Google Identity OAuth2 token with Ruby
        • Java code of AES/GCM/NoPadding encryption algorithm with authentication tag
        • How to fix error in model with gem lockbox
        • Cannot install Ruby Gem on Window
        • use logstash filter ,aes gcm encrypted in ruby,but cannot decrypted in java
        • In Rails 7, what is the right ActiveRecord callback to use if I need to prevent (or rollback) persistance on error?
        • How can I go through an array and still remove elements from it
        • Nokogiri only returning 5 results
        • How do I get the fullscreen mode in firefox?
        • undefined group option when using branch reset group regex in Ruby

        Related Questions in RUBYGEMS

        • Installing dependencies from a gemspec file
        • How to fix error in model with gem lockbox
        • rails import / require mechanism fails
        • Relation RubyGems, Ruby Version, Bundler Version, RVM and Capistrano Conflict
        • How to solve the undefine method and expand path error in Bundler
        • Error running a rails project after updating versions
        • why am i getting an 'unable to lock database: permission denied' error when installing mysql for rails 7 on windows
        • How can I change a dependencies for an installed Gem
        • While installing ruby in windows i am getting this error ERROR: Failed to build gem native extension and yaml.h not found
        • Mapkick Gem Mapbox marker icon
        • gem installation from source and naming of the gems -x86_64-linux suffix
        • Should updating rails and active record change the behavior of .days?
        • Summing odd integers from an Array in Ruby
        • How to do an advanced search in rubygems for libraries with most downloads
        • Reserved key when using I18n-tasks add-missing

        Related Questions in MECHANIZE

        • Is there a reason no links show up when using mech-dump?
        • finding username field with mechanize
        • Perl Mechanize Error GETing (Bad file descriptor)
        • Using Nokogiri to scrape element from a web page with ruby
        • How resolve a 403 Forbidden message Mechanize Ruby
        • Mechanize authentication error 401 when saving file to a shared drive
        • Python doesn't find standard default path for SSL keys on Windows
        • Attempting to select a <textarea> on a website and submit it? (Using Python Mechanize)
        • Why can't I scrape this particular website with Ruby Mechanize?
        • Python, cant use mechanize - No module named 'mechanize'
        • Web-scraping with Ruby Mechanize
        • Download file in python on clicking on href
        • python mechanize get element by id and data-row for auto-posting
        • Automating Web form Filling using Selenium or Mechanize Python with Pandas Dataframe
        • Why is mechanize not installing properly via pip on RPi? (python 3.9)

        Related Questions in MECHANIZE-RUBY

        • How to pass cookie in header of Mechanize get method?
        • Upgrading to Heroku-20, OpenSSL “dh key too small” workaround with Mechanize
        • Not able to scrape linked-in profile in production using Ruby Gems(Ruby on Rails application)
        • How to login into website using Mechanize
        • How to use Mechanize to parse local file
        • Get connection details in ruby mechanize
        • Getting Mechanize::UnauthorizedError: 401 => Net::HTTPUnauthorized when accessing API with Basic Auth
        • Mechanize Rails - Web Scraping - Server responds with JSON - How to Parse URL from to Download CSV
        • cURL works but mechanize doesn't
        • How to parse an Invalid XML
        • Ruby Mechanize scraping ResponseCodeError
        • How to avoid getting blocked by websites when using Ruby Mechanize for web crawling
        • Ruby-Mechanize: undefined method `button_with'
        • Get all tags followings a certain with mechanize ? (ruby)
        • RUBY -Mechanize::ResponseCodeError: 503 => Net::HTTPServiceUnavailable

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
        • Privacy
        • Terms
        • Cookies
        • Homegardensmart
        • Math
        • Aftereffectstemplates