A Ruby library for web scraping that uses fast static HTTP requests first and falls back to a headless browser only when JavaScript rendering is required.
gem install hybrid_scraper
# optional, for JS fallback:
gem install ferrumOr in your Gemfile:
gem "hybrid_scraper"
gem "ferrum" # optionalrequire "hybrid_scraper"
client = HybridScraper.client
result = client.fetch("https://example.com")
puts result.mode # :static or :browser
puts result.css("h1").map(&:text)result = client.fetch("https://example.com", selector: ".product-price")
# If .product-price is missing in static HTML, Ferrum is used.result = client.fetch("https://example.com") do |doc|
doc.css(".product-price").any?
enddata = client.scrape("https://example.com", {
title: "h1",
prices: ".price",
links: ->(doc) { doc.css("a").map { |a| a["href"] } }
})rows = client.scrape_many(
["https://example.com/1", "https://example.com/2"],
{ title: "h1" }
)pages = client.paginate(
"https://example.com/list",
"a.next",
limit: 5,
fields: { title: "h2" }
)HybridScraper.configure do |c|
c.user_agent = "MyBot/1.0"
c.timeout = 20
c.retries = 5
c.rate_limit = 1.0 # seconds between requests
c.headless = true
endHybridScraper.configure do |c|
c.proxy = "http://127.0.0.1:8080"
c.proxy_username = "user"
c.proxy_password = "pass"
endresult = client.fetch("https://example.com", cookies: { session: "abc" }, headers: { "Accept-Language" => "en" })HybridScraper.configure do |c|
c.rotate_user_agent = true
c.user_agents = ["Mozilla/5.0 ...", "Mozilla/5.0 ..."]
c.min_delay = 0.5
c.max_delay = 2.0
c.stealth = true
c.respect_robots = true
endclient.login(
"https://example.com/login",
username: "me",
password: "secret",
username_selector: "input[name='email']",
password_selector: "input[name='password']"
)rows = client.scrape_many(urls, { title: "h1" })
client.export_csv(rows, "out.csv")
client.export_json(rows, "out.json")- Only scrape sites you are allowed to access. Respect
robots.txtand the target site's Terms of Service. - Features like
login,stealth, andproxymust be used in accordance with the target site's rules and applicable laws. stealthadjusts browser headers/options; it is not intended to bypass anti-bot protections or access controls.- Never log or commit secrets. The library redacts common secret patterns in logs, but you are responsible for safe configuration.
- SSRF protection validates URLs and redirects, but you should still restrict usage to trusted targets.
- Ferrum is a runtime dependency for browser fallback.
- Prefer official APIs when available.