Proxy Configuration for Ruby Web Scraping: Net::HTTP, HTTParty, and Faraday
Proxy Configuration for Ruby Web Scraping: Net::HTTP, HTTParty, and Faraday
In this guide, we'll walk through setting up proxy configurations using three popular Ruby libraries for web scraping: Net::HTTP, HTTParty, and Faraday. We'll cover how to configure proxies in each library, including rotating proxies with a round-robin method. Additionally, we'll discuss parsing HTML content with Nokogiri and touch on when to use Mechanize versus raw HTTP requests.
Introduction
Web scraping can be effective for gathering data from websites, but it often requires navigating through restrictive measures like IP blocking. Proxies are essential tools in these scenarios, allowing your scraper to mimic different IP addresses and locations. In this guide, we'll explore how to configure proxies using three popular Ruby libraries: Net::HTTP, HTTParty, and Faraday. We'll also discuss rotating proxies and HTML parsing with Nokogiri.
Configuring Proxies with Net::HTTP
Net::HTTP is a low-level HTTP client library that comes bundled with Ruby. It allows you to make HTTP requests manually, which can be useful for precise control over your scraping operations.
Basic Proxy Configuration
Here's how you configure a proxy using Net::HTTP:
require 'net/http'
require 'uri'
def fetch_with_proxy(url, proxy_host, proxy_port)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts "Response code: #{response.code}"
puts "Response body: #{response.body}"
end
fetch_with_proxy('http://example.com', '127.0.0.1', 8080)
Rotating Proxies with Round-Robin
To rotate proxies using a round-robin method, you can maintain an array of proxy servers and cycle through them:
require 'net/http'
require 'uri'
proxies = [
{ host: '127.0.0.1', port: 8080 },
{ host: '127.0.0.2', port: 8081 },
{ host: '127.0.0.3', port: 8082 }
]
def fetch_with_rotating_proxy(url, proxies)
proxy_index = rand(proxies.length)
proxy_host = proxies[proxy_index][:host]
proxy_port = proxies[proxy_index][:port]
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts "Response code: #{response.code}"
puts "Response body: #{response.body}"
end
fetch_with_rotating_proxy('http://example.com', proxies)
Configuring Proxies with HTTParty
HTTParty is a higher-level client library that simplifies making HTTP requests. It provides a more intuitive interface for handling requests and responses.
Basic Proxy Configuration
To use HTTParty with a proxy, you can set the http_proxyaddr and http_proxyport options:
require 'httparty'
def fetch_with_httparty(url, proxy_host, proxy_port)
response = HTTParty.get(
url,
http_proxyaddr: proxy_host,
http_proxyport: proxy_port.to_s
)
puts "Response code: #{response.code}"
puts "Response body: #{response.body}"
end
fetch_with_httparty('http://example.com', '127.0.0.1', 8080)
Rotating Proxies with Round-Robin
To rotate proxies with HTTParty, you can use a similar approach to rotating them with Net::HTTP:
require 'httparty'
proxies = [
{ host: '127.0.0.1', port: 8080 },
{ host: '127.0.0.2', port: 8081 },
{ host: '127.0.0.3', port: 8082 }
]
def fetch_with_rotating_httparty(url, proxies)
proxy_index = rand(proxies.length)
proxy_host = proxies[proxy_index][:host]
proxy_port = proxies[proxy_index][:port]
response = HTTParty.get(
url,
http_proxyaddr: proxy_host,
http_proxyport: proxy_port.to_s
)
puts "Response code: #{response.code}"
puts "Response body: #{response.body}"
end
fetch_with_rotating_httparty('http://example.com', proxies)
Configuring Proxies with Faraday
Faraday is a highly customizable HTTP client that supports various adapters and middleware. It's particularly useful for complex requests and can be extended with custom middleware.
Basic Proxy Configuration
To use Faraday with a proxy, you can configure the adapter to use the proxy:
require 'faraday'
def fetch_with_faraday(url, proxy_host, proxy_port)
conn = Faraday.new(
url: url,
proxy: "http://#{proxy_host}:#{proxy_port}"
) do |faraday|
faraday.adapter Faraday.default_adapter
end
response = conn.get
puts "Response code: #{response.status}"
puts "Response body: #{response.body}"
end
fetch_with_faraday('http://example.com', '127.0.0.1', 8080)
Rotating Proxies with Round-Robin
To rotate proxies using Faraday, you can create a custom middleware that cycles through proxy servers:
require 'faraday'
proxies = [
{ host: '127.0.0.1', port: 8080 },
{ host: '127.0.0.2', port: 8081 },
{ host: '127.0.0.3', port: 8082 }
]
class ProxyRotator
def initialize(proxies)
@proxies = proxies
@index = 0
end
def call(env)
proxy_index = (@index % @proxies.length)
env[:request][:proxy] = "http://#{@proxies[proxy_index][:host]}:#{@proxies[proxy_index][:port]}"
@index += 1
@app.call(env)
end
end
def fetch_with_faraday_rotating(url, proxies)
conn = Faraday.new(
url: url,
request: { proxy: ProxyRotator.new(proxies) }
) do |faraday|
faraday.adapter Faraday.default_adapter
end
response = conn.get
puts "Response code: #{response.status}"
puts "Response body: #{response.body}"
end
fetch_with_faraday_rotating('http://example.com', proxies)
HTML Parsing with Nokogiri
Once you've fetched data using one of the above methods, you can parse it with Nokogiri, a popular Ruby gem for parsing HTML and XML documents.
Basic Nokogiri Parsing
Here's an example of how to use Nokogiri to parse HTML content:
require 'nokogiri'
def parse_html(html)
doc = Nokogiri::HTML(html)
title = doc.at_css('title').text
puts "Page Title: #{title}"
end
html_content = 'Example Page Hello, World!
'
parse_html(html_content)
When to Use Mechanize vs Raw HTTP
Mechanize is a higher-level library that simulates a web browser. It's useful for handling forms, cookies, and JavaScript-generated content. However, it can be slower and more resource-intensive than raw HTTP requests.
Using Mechanize
require 'mechanize'
def fetch_with_mechanize(url)
agent = Mechanize.new
page = agent.get(url)
puts "Page Title: #{page.title}"
end
fetch_with_mechanize('http://example.com')
When to Use Raw HTTP
- Performance: If you need high performance and low resource usage, raw HTTP requests are the way to go.
- Simplicity: For simple scraping tasks without complex interactions (like forms or JavaScript), raw HTTP is sufficient.
Conclusion
In this guide, we've covered how to configure proxies using Net::HTTP, HTTParty, and Faraday in Ruby. We discussed basic proxy configuration, rotating proxies with a round-robin method, and HTML parsing with Nokogiri. Additionally, we touched on when to use Mechanize versus raw HTTP requests.
If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription:
https://proxyforge.dev/#download
For professional or managed proxy services like Bright Data and SmartProxy, these providers offer reliable rotating proxies for production use.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →