If you are crawling a page that requires you to render the Javascript on the page to scrape the data you need, then we can fetch these pages using a headless browser.
To render Javascript, simply set render=true and we will use a headless Google Chrome instance to fetch the page. This feature is available on all plans.
Pass the JavaScript rendering parameter within the URL:
API REQUEST
require'net/http'require'uri'require'json'# Define parameters including headersparams = { api_key: "<YOUR_API_KEY>", url: "http://httpbin.org/ip", render: true}# URI for the API endpoint (note: use HTTPS)uri =URI('https://api.scraperapi.com/')uri.query =URI.encode_www_form(params)# Create a GET requestreq =Net::HTTP::Get.new(uri)req['Accept'] ='application/json'req['X-MyHeader'] ='123'# Create an HTTPS connectionhttp =Net::HTTP.new(uri.hostname, uri.port)http.use_ssl =truehttp.verify_mode =OpenSSL::SSL::VERIFY_PEER# Perform the HTTPS requestwebsite_content = http.request(req)# Output the response bodyputs website_content.body
# remember to install the library: gem install scraperapi (compatible with Ruby 2x)require"scraper_api"client =ScraperAPI::Client.new("APIKEY")result = client.get("http://httpbin.org/ip", render: true).raw_bodyputs result
Pass the parameter in the headers:
API REQUEST
require'net/http'require'json'# Define parameters including the target URLparams = { url: "https://httpbin.org/ip"# Specify the URL you want to scrape}# Set the API endpoint URI (HTTPS)uri =URI('https://api.scraperapi.com/')uri.query =URI.encode_www_form(params) # Encode parameters into the query string# Create a new GET requestreq =Net::HTTP::Get.new(uri)# Set Scraper API headersreq['x-sapi-render'] ='true'# Enable renderingreq['x-sapi-api_key'] ='<YOUR_API_KEY>'# Replace with your actual Scraper API key# Create an HTTPS connectionhttp =Net::HTTP.new(uri.hostname, uri.port)http.use_ssl =true# Enable SSL/TLS encryptionhttp.verify_mode =OpenSSL::SSL::VERIFY_PEER# Verify the server's certificate# Perform the HTTPS request and store the responsewebsite_content = http.request(req)# Output the response body (the scraped content)puts website_content.body
PROXY MODE
require'httparty'# Define headers with the required parametersheaders = {"x-sapi-render"=>"true"}# Set default options for HTTParty, disabling SSL verificationHTTParty::Basement.default_options.update(verify: false)# Make the HTTP request with HTTPartyresponse =HTTParty.get('http://httpbin.org/ip', { http_proxyaddr: "proxy-server.scraperapi.com", http_proxyport: "8001", http_proxyuser: "scraperapi", http_proxypass: "<YOUR_API_KEY>", headers: headers})# Capture the response bodyresults = response.body# Output the response bodyputs results
SDK Method
require"scraper_api"client =ScraperAPI::Client.new("<YOUR_API_KEY>")headers = {"x-sapi-render"=>"true"}result = client.get("http://httpbin.org/ip", headers: headers).raw_bodyputs result