草庐IT

www-mechanize

全部标签

ruby - 捕捉 Mechanize 404 => Net::HTTPNotFound

我写了一个简单的函数来处理url的获取:deftender_page_geturl,agentsleep(rand(6)+2)beginreturnagent.get(url).parserrescueErrno::ETIMEDOUT,Timeout::Error,Net::HTTPNotFoundEYE.debug"--wintersleep#{url}"puts"-x-#{url}"sleep(300)tender_page_geturl,agentrescue=>eputs"-x-#{url}"EYE.debug"--unknownexception"EYE.debug"#{ur

ruby - 如何让 Ruby Mechanize 获得一个存在于字符串中的页面

通常Mechanize将从URL获取网页,get方法的结果是一个Mechanize::Page对象,您可以从中使用很多有用的方法。如果页面存在于字符串中,我如何获得相同的Mechanize::Page对象?require'mechanize'html=PageTitleThisisatestEND_OF_STRINGagent=Mechanize.new#HowcanIgetthepageresultfromthestringhtml?#page=... 最佳答案 Mechanize使用Nokogiri来解析HTML。如果您在不需要

ruby-on-rails - 从字符串中删除 "www", "http://"

如何使用Ruby从字符串中删除“www”、“http://”、“https://”?我试过了,但没用:s.gsub('/(?:http?:\/\/)?(?:www\.)?(.*)\/?$/i','')这是我在Rails中所做的:URL被截断了,但我的目标是删除链接的开头,例如“www”或“http://”,这样链接看起来像“google.com/somepage/d...”,不像“http://google.com/some...” 最佳答案 s=s.sub(/^https?\:\/\//,'').sub(/^www./,'')如果

ruby - 使用 ruby​​ mechanize 检测重定向

我正在使用mechanize/nokogirigems来解析一些随机页面。我在301/302重定向方面遇到问题。这是代码片段:agent=Mechanize.newpage=agent.get('http://example.com/page1')mydomain.com上的测试服务器会将页面1重定向到页面2,状态代码为301/302,因此我期望有page.code=="301"相反,我总是得到page.code=="200"。我的要求是:我希望遵循重定向(默认Mechanize行为,这很好)我希望能够检测到该页面实际上已被重定向我知道我可以在agent.history中看到page1

ruby - 使用 Ruby Mechanize 单击按钮

我有一个特别困难的表单,我试图单击搜索按钮但似乎无法执行。这是页面源代码中的表单代码:我正在尝试执行标准的Mechanize点击操作:login_page=agent.click(homepage.link_with(:text=>"Search"))这是因为按钮使用了javascript吗?如果是这样,有什么建议吗? 最佳答案 我也为此苦苦挣扎,尤其是因为我的表单有多个按钮。提交表单的方式有多种(许多使用“form_with”block),但这对我有帮助:#gettheformform=agent.page.form_with(:

ruby - 在 Mechanize 请求之间维护 cookie

我正在尝试使用Mechanize的Ruby版本从我们正在远离的不提供API的工单管理系统中提取我雇主的工单。问题是,Mechanize似乎没有在post调用和get调用之间保留cookie,如下所示:require'rubygems'require'nokogiri'require'mechanize'@agent=Mechanize.newpage=@agent.post('http://.com/user_session',{'authenticity_token'=>'','user_session[login]'=>'','user_session[password]'=>''

ruby - Mechanize 如何获取当前 url

我有这个代码require'mechanize'@agent=Mechanize.newpage=@agent.get('http://something.com/?page=1')next_page=page.link_with(:href=>/^?page=2/).click如您所见,此代码应转到下一页。next_page应该有urlhttp://something.com/?page=2如何获取next_page的当前url? 最佳答案 next_page.uri.to_s参见http://www.rubydoc.info/g

ruby - 使用 WWW :Mechanize to download a file to disk without loading it all in memory first

我正在使用Mechanize来简化某些文件的下载。目前我的脚本使用以下行来实际下载文件...agent.get('http://example.com/foo').save_as'a_file_name'然而,这会将完整的文件下载到内存中,然后再将其转储到磁盘。你如何绕过这种行为,直接下载到磁盘?如果我需要使用WWW:Mechanize以外的东西,那么我将如何使用WWW:Mechanize的cookies呢? 最佳答案 您真正想要的是Mechanize::Downloadhttp://mechanize.rubyforge.org/

ruby - 如何测试使用 mechanize 的 ruby​​ 应用程序

我编写了一个使用Mechanize遍历站点的小程序。我想为它编写测试,但不希望它在我每次运行测试时都实际登录该站点。我想模拟互联网,这样当它访问某个站点时,它只会返回存储的结果。这是一个小例子,假装我的代码的目的是从google主页中提取链接,所以我编写了一个测试以确保我的代码找到的第一个链接包含文本“图像”。我可能会这样写:require'rubygems'require'mechanize'require'test/unit'defmy_code_to_find_linksgoogle=WWW::Mechanize.new.get('http://www.google.com')#

ruby Mechanize : user agents?

Mechanize中有多少用户代理?是否有所有用户代理选项的便捷列表? 最佳答案 是的。看https://github.com/sparklemotion/mechanize/blob/master/lib/mechanize.rb#L115:AGENT_ALIASES={'WindowsIE6'=>'Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1)','WindowsIE7'=>'Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;.NETCLR1.1.4