Ruby Mechanize, Nokogiri and Net::HTTP

南笙酒味 提交于 2019-12-04 14:58:02

I'm not sure why you think using Net::HTTP would be better. Mechanize will handle redirects and cookies, plus provides ready access to Nokogiri's parsed document.

require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.example.com')

# Use Nokogiri to find the content of the <h1> tag...
puts page.at('h1').content # => "Example Domains"

Note, setting the user_agent isn't necessary to reach example.com.


If you want to use a threaded engine to retrieve pages, take a look at Typhoeous and Hydra.

Looks like Mechanize has a parse method, so this could work:

mechanize_obj = Mechanize.parse(uri, response, body)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!