机械化如何获取当前网址 [英] mechanize how to get current url

查看:84
本文介绍了机械化如何获取当前网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

require 'mechanize'
@agent = Mechanize.new
page = @agent.get('http://something.com/?page=1')
next_page = page.link_with(:href=>/^?page=2/).click

如您所见,此代码应转到下一页.

As you can see this code should go to the next page.

next_page应具有URL http://something.com/?page=2

如何获取next_page的当前网址?

How to get current url for next_page?

推荐答案

next_page.uri.to_s 

请参见 http://www.rubydoc.info/gems/mechanize/Mechanize/Page/Link#uri-instance_method http://ruby-doc.org/stdlib-2.4.1/libdoc/uri/rdoc/URI.html

出于测试目的,我在irb中执行了以下操作:

For testing purposes, I did the following in irb:

require 'mechanize'
@agent = Mechanize.new

page = @agent.get('http://news.ycombinator.com/news')
=> #<Mechanize::Page
 {url #<URI::HTTP:0x00000001ad3198 URL:http://news.ycombinator.com/news>}
 {meta_refresh}
 {title "Hacker News"}
 {iframes}
 {frames}
 {links
  #<Mechanize::Page::Link "" "http://ycombinator.com">
  #<Mechanize::Page::Link "Hacker News" "news">
  #<Mechanize::Page::Link "new" "newest">
  #<Mechanize::Page::Link "comments" "newcomments">
  #<Mechanize::Page::Link "ask" "ask">
  #<Mechanize::Page::Link "jobs" "jobs">
  #<Mechanize::Page::Link "submit" "submit">
  #<Mechanize::Page::Link "login" "newslogin?whence=%6e%65%77%73">
  #<Mechanize::Page::Link "" "vote?for=3803568&dir=up&whence=%6e%65%77%73">
  #<Mechanize::Page::Link
   "Don’t Be Evil: How Google Screwed a Startup"
   "http://blog.hatchlings.com/post/20171171127/dont-be-evil-how-google-screwed-a-startup">
  #<Mechanize::Page::Link "mikeknoop" "user?id=mikeknoop">
  #<Mechanize::Page::Link "64 comments" "item?id=3803568">
  #<Mechanize::Page::Link "" "vote?for=3802515&dir=up&whence=%6e%65%77%73">
  # Omitted for brevity...

next_page.uri
 => #<URI::HTTP:0x00000001fa7818 URL:http://news.ycombinator.com/news2> 

next_page.uri.to_s
 => "http://news.ycombinator.com/news2" 

这篇关于机械化如何获取当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆