如何使用socksify gem(代理)抓取网站 [英] How to scrape a website with the socksify gem (proxy)

查看:103
本文介绍了如何使用socksify gem(代理)抓取网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于 Rubyforge 的 socksify gem 的文档.我已成功安装 gem,并成功运行此文档代码以测试我的本地实现是否可以复制它:

I am reading through the documentation of the socksify gem on Rubyforge. I have installed the gem successfully, and I have run this documented code with success to test that my local implementation can replicate it:

require 'socksify/http'
uri = URI.parse('http://rubyforge.org/')
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
  http.get(uri.path)
end
# => #<Net::HTTPOK 200 OK readbody=true>

但是我该怎么做,例如抓取 'http://google.com/',并获取 html 内容?我想用例如解析它像这样的Nokogiri:

But how do I e.g. scrape 'http://google.com/', and get the html content? I wish to parse it with e.g. Nokogiri like this:

Nokogiri::HTML(open("http://google.com/))

推荐答案

 require 'socksify/http'
 http = Net::HTTP::SOCKSProxy(addr, port)
 html = http.get(URI('http://google.de'))
 html_doc = Nokogiri::HTML(html)

这篇关于如何使用socksify gem(代理)抓取网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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