Ruby: tutorial on using the mechanism

Source: Internet
Author: User

Tips

 
  • Puts Mechanic: AGENT_ALIASES can print out all available user_agent
  • Puts mechanic.instance_methods(false)Output all methods of the Mechanism module
  • Puts mechanic. instance_methods () Outputs all methods of the mechanic module and the functions of the inherited classes.
Recommended reading

Official documentation

Many machize Examples

Simulate Google Search

Analyze and batch download School Intranet album photos using the mechanism

Mechanical ize user manual Chinese Version

Machize User Manual (English version)

Some common methods to simulate natural web page interaction by using the mechanism

In Ruby, web page capturing is usually implemented using the mechanism, which is very simple to use.

Install

sudo gem install mechanize

Capture webpages

require == agent.get()

Simulate click events

page = agent.page.link_with(:text => ).click

Simulate form submission

google_form = page.form(] = =

Analysis page. The mechanism uses nokogiri to parse the webpage. Therefore, you can refer to the nokogiri document.

table = page.search(=

Note: If you need to log on to the webpage first, you can log on to the website first, log on to the JSESSIONID, and assign it to the agent.

cookie = Mechanize::Cookie.new(, = = 

If you need to save the webpage, use. save_as (or save, I have not tried it). For example:

agent.get().save_as

 

Reprinted from: http://www.cnblogs.com/Stoned/archive/2012/02/23/2364389.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.