Mechanize例子

Mechanize例子Thislistbase german article Webscrapingm Mechanize http sixserv org 2009 05 27 webscripting mit ruby und mechanize 00Initializa rubygems require mechanize

This list based on my (german) article: "Web scraping mit Ruby/Mechanize" http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/ #00 Initialization
require 'rubygems' require 'mechanize' agent = WWW::Mechanize.new agent.set_proxy('localhost', '8000') agent.user_agent = 'Individueller User-Agent' agent.user_agent_alias = 'Linux Mozilla' agent.open_timeout = 3 agent.read_timeout = 4 agent.keep_alive = false agent.max_history = 0 # reduce memory if you make lots of requests
#01 manual get requests
url = 'http://apoc.sixserv.org/requestinfo/' page = agent.get url # or ... page = agent.get(url, {"name" => "value", "key" => "val"})
#02 manual post submits
url = 'http://apoc.sixserv.org/requestinfo/' page = agent.post(url, {"name" => "value", "key" => "val"})
#03 form post submits
page = agent.get 'https://twitter.com/login' login_form = page.form_with(:action => 'https://twitter.com/sessions') login_form['session[username_or_email]'] = '[Username]' login_form['session[password]'] = '[Password]' page = agent.submit login_form
#04 link and history navigation
page = agent.get 'http://www.heise.de/' page = agent.click(page.link_with(:text => /Telepolis/)) page = agent.click(page.link_with(:href => /artikel/)) agent.back agent.back puts page.body
#05 exceptions
begin page = agent.get 'http://apoc.sixserv.org/diese/seite/gibt/es/nicht/' rescue WWW::Mechanize::ResponseCodeError puts "ResponseCodeError - Code: #{$!}" end
#06 referer
page = agent.get(:url => 'http://apoc.sixserv.org/requestinfo/', :referer => 'http://google.com/this/is/a/custom/referer') puts page.body
#07 request header manipulation
agent.pre_connect_hooks << lambda do |params| params[:request]['X-Requested-With'] = 'XMLHttpRequest' end
#08 response header
page = agent.head 'http://sixserv.org' server_version = page.header['server'] puts "Server: #{server_version}" if page.header.key? 'x-powered-by' php_version = page.header['x-powered-by'] puts "X-Powered-By: #{php_version}" end # redirection urls: agent.redirect_ok = false page = agent.get 'http://www.sixserv.org/' puts page.header['location']
#09 content parsing
# X Path / CSS-Selector:
page = agent.get 'http://xkcd.com/'
img = page.search '/html/body/div/div[2]/div/div[2]/div/div/img'
puts img
# Regular Expression:
page = agent.get 'http://example.com/'
page.body.match /< h3>([^<]+)< \/h3>/
puts "Heading 3: #{$1}"
#10 "with" method examples
# *_with: form, link, base, frame or iframe # get the first link including "foo" inside url: page.link_with(:href => /foo/) # all links with text 'more' page.links_with(:text => 'more') # get the form with the name 'foo' page.form_with('foo') # or form_with(:name => 'foo')

 
 
 
 
 
 
 
 
 
 
 
 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214150.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午4:52
下一篇 2026年3月18日 下午4:52


相关推荐

  • 微信公众号平台开发教程_微信公众号制作教程

    微信公众号平台开发教程_微信公众号制作教程关键字:微信公众平台开发入门作者:贝创工作室原文:http://www.cnblogs.com/imaker/p/5491433.html背景知识:微信公众平台的开发需要有一定的PHP基础知识,PHP是一种HTML内嵌式的语言,广泛用于网站开发。微信服务器和开发者的服务器间有两种数据传输方式,分别是XML和JSON。其中XML主要用在接受发送普通消息和事件推送…

    2025年5月23日
    6
  • 机器视觉:ransac算法详解

    机器视觉:ransac算法详解一维度的 ransac 算法 直线拟合 可以构成曲线拟合 或者曲面拟合 usr bin envpython coding utf 8 Time 2021 1 418 42 Author shaocanfan File Ransac pyimportnump pyplotasplti 数据量 SIZE 50

    2026年3月16日
    2
  • Windows 环境下查看 Redis 版本号命令「建议收藏」

    Windows环境下查看Redis版本号命令://Redis客户端输入infoserver//结果#Serverredis_version:3.2.100redis_git_sha1:00000000redis_git_dirty:0redis_build_id:dd26f1f93c5130eeredis_mode:standaloneos:Window…

    2022年4月9日
    472
  • 定价

    定价

    2026年3月16日
    3
  • nginx防火墙问题

    nginx防火墙问题记录一次找了许久的问题最开始是以为 setCookie 储存不了的问题 换了 serStorage 也不行两个项目同时都有这个问题 输入账号密码登录跳转不到首页一个项目安卓和 windows 都能打开 就是苹果浏览器不行一个项目是都不行最后发现是 nginx 没有开防火墙开启之后没有做任何改变就都好了 firewalld 的基本使用启动 systemctlsta 关闭 systemctlsto 查看状态 systemctlsta

    2026年3月19日
    2
  • openclaw免费集成搜索功能

    openclaw免费集成搜索功能

    2026年3月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号