使用Mechanize发布百度空间博客_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > 使用Mechanize发布百度空间博客

使用Mechanize发布百度空间博客

 2010/9/19 23:19:02  holin  http://holin.javaeye.com  我要评论(0)
  • 摘要:官网:http://mechanize.rubyforge.org登录require'rubygems'require'mechanize'require'logger'#Mechanize.log=Logger.new(STDOUT)agent=Mechanize.new#ppagent.methods.sort.grep(/agent/)agent.user_agent_alias='MacSafari'#ppagent.user_agentpage=agent.get('https
  • 标签:使用Mechanize发布百度空间博客

官网:http://mechanize.rubyforge.org

?

登录

require 'rubygems'
require 'mechanize'
require 'logger'
# Mechanize.log = Logger.new(STDOUT)
agent = Mechanize.new
# pp agent.methods.sort.grep(/agent/)
agent.user_agent_alias = 'Mac Safari'
# pp agent.user_agent
page = agent.get('https://passport.baidu.com/?login')

?

跑出异常

/Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/chain/body_decoding_handler.rb:39:in `handle': Unsupported content encoding (RuntimeError)
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/chain.rb:29:in `pass'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/chain/handler.rb:6:in `handle'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/chain/response_reader.rb:35:in `handle'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/chain.rb:24:in `handle'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize.rb:543:in `fetch_page'
    from /Users/holin/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:1054:in `request'
    from /Users/holin/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:2144:in `reading_body'
    from /Users/holin/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/http.rb:1053:in `request'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize.rb:538:in `fetch_page'
    from /Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize.rb:259:in `get'
    from hi_baidu.rb:13

?

主要是百度返回的response['Content-Encoding']是none,于是修改monospace; vertical-align: baseline; white-space: pre; padding: 0px; margin: 0px;">mechanize/chain/body_decoding_handler.rb,34行:

when 'x-gzip', 'none'

?通过!!

继续:

equire 'rubygems'
require 'mechanize'
require 'logger'
# Mechanize.log = Logger.new(STDOUT)
agent = Mechanize.new
# pp agent.methods.sort.grep(/agent/)
agent.user_agent_alias = 'Mac Safari'
# pp agent.user_agent
page = agent.get('https://passport.baidu.com/?login')
# pp page
login_form = page.forms.first
# pp login_form
login_form.username = "我爱游记"
login_form.password = "mypassword"
page = agent.submit(login_form)
page = agent.get("http://hi.baidu.com/%E6%88%91%E7%88%B1%E6%B8%B8%E8%AE%B0/creat/blog/")
form = page.form("form1")
pp form.methods.sort
form.spBlogTitle = "我最爱游记了,呵呵"
form.spBlogText = "我会发很多游记的,呵呵"
page = agent.submit(form)
?

运行后抛异常:

/Users/holin/.rvm/gems/ruby-1.8.7-p299@rails2/gems/mechanize-1.0.0/lib/mechanize/form.rb:136:in `method_missing': undefined method `spBlogText=' for #<Mechanize::Form:0x10188b7b8> (NoMethodError)
    from hi_baidu.rb:31
?

spBlogText 文本框是js生成的,怎么办?

想办法生成这个文本框!

......
form.add_field!("spBlogText") #自己增加Field
form.spBlogTitle = "我最爱游记了,呵呵"
form.spBlogText = "我会发很多游记的,呵呵"
....
?成功了,哈哈。

?

可以访问地址:http://hi.baidu.com/%CE%D2%B0%AE%D3%CE%BC%C7/blog/item/de9e0b4aa2facf07b2de051a.html

?

首发:http://www.holin.info/posts/4c7b6a2aff1f015018000001

?

:)

?

  • 相关文章
发表评论
用户名: 匿名