### Initialize and Use Crawlbase Leads API Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This snippet demonstrates how to initialize the `LeadsAPI` client with its dedicated token. It includes an example of making a GET request to retrieve lead-related information for a given domain, showcasing the specific functionality of the Leads API. ```java LeadsAPI leadsApi = new LeadsAPI("YOUR_TOKEN"); ``` ```java leadsApi.get("stripe.com"); System.out.println(leadsApi.getStatusCode()); System.out.println(leadsApi.getBody()); ``` -------------------------------- ### Perform GET Requests with Crawlbase API Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md These examples illustrate how to execute GET requests using the initialized Crawlbase API client to scrape web pages. They cover basic GET calls and advanced usage with custom options, such as specifying a user agent or requesting JSON format, to tailor the scraping process. ```java api.get(url, options); ``` ```java api.get("https://www.facebook.com/britneyspears"); System.out.println(api.getStatusCode()); System.out.println(api.getOriginalStatus()); System.out.println(api.getCrawlbaseStatus()); System.out.println(api.getBody()); ``` ```java HashMap options = new HashMap(); options.put("user_agent", "Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/30.0"); options.put("format", "json"); api.get("https://www.reddit.com/r/pics/comments/5bx4bx/thanks_obama/", options); System.out.println(api.getStatusCode()); System.out.println(api.getBody()); ``` -------------------------------- ### Initialize and Use Crawlbase Scraper API Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This section details the initialization of the `ScraperAPI` client using a specific token. It provides an example of how to make a GET request to scrape structured data, such as product information from an e-commerce site, leveraging the specialized capabilities of the Scraper API. ```java ScraperAPI scraperApi = new ScraperAPI("YOUR_TOKEN"); ``` ```java scraperApi.get(url, options); ``` ```java scraperApi.get("https://www.amazon.com/Halo-SleepSack-Swaddle-Triangle-Neutral/dp/B01LAG1TOS"); System.out.println(scraperApi.getStatusCode()); System.out.println(scraperApi.getBody()); ``` -------------------------------- ### Use Crawlbase Javascript API for Dynamic Content Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This section explains how to initialize the API client with a dedicated Javascript token for scraping dynamic websites built with frameworks like React or Angular. It demonstrates making GET requests and applying specific options, such as `page_wait`, to control the rendering process. Note that only GET requests are supported for the Javascript API. ```java API api = new API("YOUR_JAVASCRIPT_TOKEN"); ``` ```java api.get("https://www.nfl.com"); System.out.println(api.getStatusCode()); System.out.println(api.getBody()); ``` ```java HashMap options = new HashMap(); options.put("page_wait", "5000"); api.get("https://www.freelancer.com", options); System.out.println(api.getStatusCode()); ``` -------------------------------- ### Capture Screenshot and Save to Specific Path Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md Illustrates how to use the Crawlbase Screenshots API to capture a screenshot and save it directly to a specified local file path. This example utilizes a HashMap to pass configuration options, such as 'save_to_path', to the API call. ```java ScreenshotsAPI screenshotsApi = new ScreenshotsAPI("YOUR_TOKEN"); HashMap options = new HashMap(); options.put("save_to_path", "/home/my-dir/apple.jpg"); screenshotsApi.get("https://www.apple.com", options); System.out.println(screenshotsApi.getStatusCode()); System.out.println(screenshotsApi.getScreenshotPath()); ``` -------------------------------- ### Perform POST Requests with Crawlbase API Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md These snippets demonstrate how to send POST requests with data using the Crawlbase API client. Examples include sending form-urlencoded data and explicitly setting the `post_content_type` option to 'json' for sending JSON payloads, providing flexibility for various API interactions. ```java api.post(url, data, options); ``` ```java HashMap data = new HashMap(); data.put("text", "example search"); api.post("https://producthunt.com/search", data); System.out.println(api.getStatusCode()); System.out.println(api.getBody()); ``` ```java HashMap data = new HashMap(); data.put("some_json", "with some value"); HashMap options = new HashMap(); options.put("post_content_type", "json"); api.post("https://httpbin.org/post", data, options); System.out.println(api.getStatusCode()); System.out.println(api.getBody()); ``` -------------------------------- ### Initialize Crawlbase API Client Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This code shows how to instantiate the main `API` class using your unique Crawlbase account token. This token is crucial for authenticating all subsequent requests made through the API client, enabling access to Crawlbase's web scraping services. ```java API api = new API("YOUR_TOKEN"); ``` -------------------------------- ### Capture Screenshot with Crawlbase Java Screenshots API Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md Demonstrates how to initialize the Crawlbase Screenshots API with your token and capture a screenshot of a given URL. It also shows how to retrieve the HTTP status code and the path where the screenshot is saved after the API call. ```java ScreenshotsAPI screenshotsApi = new ScreenshotsAPI("YOUR_TOKEN"); screenshotsApi.get("https://www.apple.com"); System.out.println(screenshotsApi.getStatusCode()); System.out.println(screenshotsApi.getScreenshotPath()); ``` -------------------------------- ### Import Crawlbase Java Library Classes Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This snippet demonstrates how to import the necessary classes from the `java.util` and `com.crawlbase` packages. These imports are essential for setting up and utilizing the Crawlbase API client in any Java project. ```java import java.util.*; import com.crawlbase.*; ``` -------------------------------- ### Decode Base64 Screenshot Body to Bytes Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md Shows how to convert the Base64 string representation of the binary image file, which is returned by the Crawlbase Screenshots API's `getBody()` method, into a byte array. This is useful for processing the image data directly in Java. ```java byte[] data = Base64.getDecoder().decode(screenshotsApi.getBody()); ``` -------------------------------- ### Access Crawlbase API Response Status Codes Source: https://github.com/crawlbase/crawlbase-java/blob/main/README.md This snippet demonstrates how to retrieve various status codes from the API response after a request. It shows how to access the original HTTP status from the target server and the Crawlbase-specific status, which are useful for debugging and understanding the outcome of a scraping operation. ```java api.get("https://sfbay.craigslist.org/"); System.out.println(api.getOriginalStatus()); System.out.println(api.getCrawlbaseStatus()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.