Skip to content

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

HybridScraper

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.

Installation

gem install hybrid_scraper
# optional, for JS fallback:
gem install ferrum

Or in your Gemfile:

gem "hybrid_scraper"
gem "ferrum" # optional

Usage

Basic fetch

require "hybrid_scraper"

client = HybridScraper.client
result = client.fetch("https://example.com")

puts result.mode   # :static or :browser
puts result.css("h1").map(&:text)

Force browser fallback with a selector

result = client.fetch("https://example.com", selector: ".product-price")
# If .product-price is missing in static HTML, Ferrum is used.

Force browser fallback with a block

result = client.fetch("https://example.com") do |doc|
  doc.css(".product-price").any?
end

Structured scraping

data = client.scrape("https://example.com", {
  title: "h1",
  prices: ".price",
  links: ->(doc) { doc.css("a").map { |a| a["href"] } }
})

Many URLs

rows = client.scrape_many(
  ["https://example.com/1", "https://example.com/2"],
  { title: "h1" }
)

Pagination

pages = client.paginate(
  "https://example.com/list",
  "a.next",
  limit: 5,
  fields: { title: "h2" }
)

Configuration

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
end

Advanced features

Proxy

HybridScraper.configure do |c|
  c.proxy = "http://127.0.0.1:8080"
  c.proxy_username = "user"
  c.proxy_password = "pass"
end

Cookies & headers

result = client.fetch("https://example.com", cookies: { session: "abc" }, headers: { "Accept-Language" => "en" })

Anti-bot helpers

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
end

Login (form-based)

client.login(
  "https://example.com/login",
  username: "me",
  password: "secret",
  username_selector: "input[name='email']",
  password_selector: "input[name='password']"
)

Export

rows = client.scrape_many(urls, { title: "h1" })
client.export_csv(rows, "out.csv")
client.export_json(rows, "out.json")

Security, privacy, and terms

  • Only scrape sites you are allowed to access. Respect robots.txt and the target site's Terms of Service.
  • Features like login, stealth, and proxy must be used in accordance with the target site's rules and applicable laws.
  • stealth adjusts 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.

Notes

  • Ferrum is a runtime dependency for browser fallback.
  • Prefer official APIs when available.

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages