Skip to main content
Web Scraping

Published Oct 21, 2026

How Do You Start Web Scraping With Java?

Learn Java web scraping with jsoup. Fetch pages, parse HTML, follow pagination, configure proxies, validate records, and scale safely.

How Do You Start Web Scraping With Java?

Quick Answer

Web scraping with Java usually starts with jsoup because it can request and parse static HTML. Set a page limit, pace requests, and define the output before collecting data. Add Selenium when the required content genuinely depends on browser execution.

bash

Key Takeaways

  • jsoup combines static-page retrieval, tolerant HTML parsing, and CSS selectors in one Java library.
  • The tutorial builds a paginated scraper that writes complete hockey records to CSV.
  • Selenium belongs in the stack when JavaScript or browser interaction produces the required data.
  • Proxy settings belong in the request layer and should reflect a specific location or routing requirement.
  • Larger crawls need finite queues, per-host pacing, controlled retries, duplicate handling, and useful logs.
  • Cost per complete record reveals more than raw request counts or HTTP status totals.

Which Java Web Scraping Tool Should You Choose?

The best Java scraping tool depends on the source format and the work required before extraction. Static HTML often needs jsoup alone, while rendered interfaces need a browser.

ToolPrimary roleBest forMain limitation
JDK `HttpClient`HTTP clientAPIs, custom request flows, asynchronous requests, and transport controlDoes not parse HTML
jsoupHTTP client and HTML parserStatic pages, CSS selectors, links, forms, and document traversalDoes not execute page JavaScript
SeleniumBrowser automationRendered pages and browser interactionsRequires more memory, browser setup, and wait handling

The JDK `HttpClient` provides detailed transport control for APIs and custom request flows. Pair it with a parser when the response contains HTML.

The jsoup documentation covers requests, parsing, CSS selectors, relative links, cookies, and configuration. jsoup is the practical default for this tutorial because one dependency handles static retrieval and HTML parsing.

Selenium controls a browser, executes JavaScript, and supports interaction with rendered interfaces. The headless browser guide explains its additional setup and operating costs.

The broader web scraping guide explains how retrieval, parsing, and storage fit together. Those responsibilities remain distinct even when one Java library covers several stages.

What Do You Need Before Scraping With Java?

The Java sample requires Java 21+, Maven 3.9.0 or later, and a target whose structure you can inspect. Decide the output fields and crawl limits before creating the project.

Create the standard Maven directories:

bash

Maven expects application source under `src/main/java`, following its standard directory layout. Confirm both tools before creating the project:

bash

This tutorial uses `https://www.scrapethissite.com/pages/forms/`, a public practice site with static hockey tables. The project collects each team's name, year, wins, losses, and source page. A row enters the CSV after all five values are present.

Inspect one row and the next-page link before coding. The sample uses `tr.team`, `.name`, `.year`, `.wins`, `.losses`, and `a[aria-label=Next]`. Replace those selectors when adapting the project to another source.

Install a full JDK because Maven needs the included compiler. Replace the user-agent placeholder with a contact URL you control before changing the target.

Save a representative HTML response after removing sensitive values. That fixture keeps selector tests independent of network availability and changing content.

The web scraping with proxies guide explains where routing fits after direct extraction works. Establish the direct baseline before testing another route.

How Do You Configure Maven for Java Web Scraping?

Maven records the Java release, jsoup version, compiler settings, and executable entry point for this project. Keeping that configuration with the source makes the build repeatable.

Create `pom.xml` in the project directory:

bash

The sample pins jsoup 1.23.2 for a repeatable build. Check the jsoup release notes before adopting another version.

The `maven.compiler.release` property compiles against Java 21's public API and emits matching class files. `project.build.sourceEncoding` keeps source handling consistent across machines. jsoup supplies the request and parsing features used below.

The compiler plugin enforces the release setting. The exec plugin identifies the class containing `main()` for the run command. Production builds should also restrict downloads to trusted repositories.

Run `mvn dependency:tree` when investigating version conflicts. Commit the POM with the source, and review version changes during scheduled maintenance.

Run `mvn -q verify` before execution. Maven will resolve dependencies, compile the source, and run any configured checks before the scraper contacts the target.

How Do You Build a Java Web Scraper With jsoup?

A small jsoup scraper can keep requests, parsing, pagination, and CSV output in one class. The sample stays readable by giving each responsibility a separate method.

How Does the Java Scraper Set Its Crawl Boundaries?

The Java scraper defines one allowed origin, a page ceiling, redirect limits, a body cap, and request pacing. Its crawl loop also tracks requested and redirected page URLs.

Create `src/main/java/example/JavaScraper.java` from the five ordered blocks below. Together, they form one class.

bash

The `seen` set catches repeated page URLs, while `MAX_PAGES` catches endless sequences of distinct links. Together, they cover two common pagination failures.

`REQUEST_DELAY` adds a 750-millisecond pause between pages. Replace that value with the documented pace for the intended source.

How Does the Java Scraper Create Requests and Validate Responses?

The request layer applies shared limits, optional proxy settings, manual redirects, and response checks. It accepts HTML responses from the configured origin within the five MiB cap.

Add the request and response methods after `scrape()`:

bash

`Jsoup.newSession()` retains settings and cookies across requests. Calling `newRequest()` creates each request from that shared configuration without rebuilding the session.

Manual redirect handling keeps each destination on the allowed HTTPS origin. Status, content-type, and body-size checks run before parsing begins.

How Does the Java Scraper Extract Records and Follow Pagination?

The parser extracts the required team fields and resolves the next-page link against the response URL. Supporting helpers normalize references, enforce the origin, and recognize accepted HTML media types.

Add the extraction, pagination, and URL validation methods next:

bash

An incomplete row stops the run with its source URL in the error. Accepted records retain that URL for later tracing.

How Does the Java Scraper Write CSV Output?

The output methods quote every CSV field and write UTF-8 text. Quoting preserves commas, line breaks, and quotation marks inside field values.

Add the output methods and data records after the URL helpers:

bash

How Does the Java Scraper Validate Proxy Settings?

The proxy record reads optional settings from environment variables before the first request. It accepts either host and port alone or a complete username-and-password pair.

Add the proxy record last, then close `JavaScraper`:

bash

How Do You Configure a Proxy for Java Web Scraping?

The Java scraper reads proxy settings from environment variables before creating its shared jsoup session. This keeps credentials out of the source and supports either credentials or source-IP allowlisting.

Run the scraper with an authenticated HTTP proxy:

bash

Leave the credential variables unset for source-IP authentication. The code reports incomplete host, port, or credential combinations during startup.

An HTTP proxy can carry an HTTPS destination through CONNECT. Java validates the destination certificate after establishing that tunnel. The two URL schemes describe separate connections.

Java commonly disables Basic authentication for CONNECT by default. jsoup documents the `jdk.http.auth.tunneling.disabledSchemes` property for this case. The sample changes it before creating an authenticated session.

That property affects the entire JVM, so run the scraper in a controlled environment with a trusted endpoint. Keep certificate checks enabled while troubleshooting proxy failures.

Browser automation needs its own proxy configuration because Selenium runs outside the jsoup session. The proxy rotation strategy guide explains how connection reuse and session settings affect exit changes.

How Do You Run and Validate the Java Scraper?

Run the Java scraper directly, inspect its CSV, and then repeat the same job through the intended proxy. Comparing those runs separates extraction problems from routing problems.

  1. Compile the project: Run Maven's verification phase to resolve dependencies and check the Java source.
  2. Create a direct baseline: Execute the scraper without proxy variables and inspect every required column.
  3. Test the proxy route: Add the intended endpoint and authentication method, then rerun the same dataset.
  4. Compare valid output: Check record counts, field completeness, source pages, locale, and expected content.
bash

The verified practice-site run created `teams.csv` with 582 records. Keep that count as a test expectation and update it after reviewing any source change.

Open the first and last rows, confirm all five columns, and search for empty values. Also compare a few records with their saved source URLs. A consent page or wrong locale can still arrive with HTTP 200.

Treat extracted text as untrusted when opening CSV files in spreadsheet software. Import fields as text, or neutralize formula prefixes before opening the file.

The sample has no application retry loop. Retries belong in a failure policy that distinguishes temporary transport problems from permanent errors.

Keep CSV output separate from diagnostic logs. Record status, timing, and failure details without credentials, cookies, or sensitive query values.

When Does Java Web Scraping Need a Browser?

Java web scraping needs a browser when required content depends on JavaScript or browser interaction. Compare the original response with the rendered page before adopting Selenium.

Some sites return an application shell to jsoup and load data later. Others expose the same values through JSON, embedded state, or a documented API. Direct retrieval is usually simpler and less expensive.

Selenium manages browser state and exposes rendered elements. Tie explicit waits to a selector, response, or application state instead of a fixed delay. The official Selenium waiting guidance warns against mixing implicit and explicit waits.

Browser automation adds binaries, drivers, processes, memory use, and rendering time. Plan for version compatibility and reliable process cleanup before adding browser workers.

Proxy configuration belongs in the browser options or profile. Keep one WebDriver session on one sticky route during a related multi-step flow. Start another session when the route policy changes.

A bounded browser queue can handle rendered pages while jsoup processes static pages. This split saves resources and isolates browser-specific failures.

Source inspection should determine whether the browser path is necessary. Treat a challenge, login prompt, or unexpected page as a distinct result rather than extracted data.

How Should Java Scraping Errors Be Handled?

Java scraping errors become easier to resolve when the failing layer is clear. Classify network, proxy, target, parser, and data failures before changing retries or routes.

SymptomLikely layerFirst check
Connection timeoutNetwork, proxy, DNS, or destinationTest each route and timeout stage separately
`407` responseProxy authenticationVerify the configured method and current credentials
`429` responseDestination rate policyHonor `Retry-After` and reduce request pressure
HTTP 200 without recordsPage type, selector, locale, or soft blockCheck expected markers and save safe diagnostic metadata
Repeated page URLPaginationStop and inspect link resolution or source markup
Partial final pageBody limit, response failure, or parser ruleCheck response size, required markers, and row validation

Temporary transport failures may justify a small retry limit with exponential backoff and jitter. Preserve the final failure class after the attempt budget ends.

An unchanged `407` points to authentication, not a transient route problem. Repeated `401`, `403`, or challenge responses call for an access review.

Investigate selector failures with sanitized HTML fixtures. Repeatable parser tests can then cover markup changes without another live request.

Measure complete records instead of accepted responses. The how to test proxies guide separates gateway health, route behavior, target acceptance, and content quality.

How Do You Scale Java Web Scraping Safely?

Scaled Java scraping needs bounded workers, per-host pacing, deduplicated queues, and controlled retry budgets. Available threads say nothing about a destination's acceptable request rate.

Place crawl URLs into a finite queue. Use fixed platform threads or virtual threads behind an explicit concurrency limit. Virtual threads reduce blocked-thread overhead, while sockets and downstream systems remain finite.

Set a separate request rate for each host because response times and published limits differ. A global worker count cannot represent those source-specific constraints.

Normalize URLs before deduplication while preserving parameters that identify distinct records. Persist crawl state when a restart must resume unfinished work.

Assign each retry class to one layer. Overlapping client, worker, queue, and scheduler retries can multiply attempts unexpectedly. Record the shared attempt count with each task.

Track latency, bytes, response type, retries, route, extracted records, and rejection reasons. Use those measurements to calculate the useful outcome:

bash

Increase the workload gradually and pause when latency, `429` responses, challenges, or rejected records cross defined thresholds. The proxy pool guide covers route health and replacement decisions.

Cache unchanged responses and skip unnecessary browser assets when the page still behaves correctly. Both controls reduce destination load, compute costs, and proxy bandwidth.

What Rules Should a Java Scraper Follow?

A Java scraper should collect authorized data at a rate suitable for each source. Access can change, so ownership of permission and policy review must be clear.

Review applicable law, website terms, robots instructions, and API options before scheduling a source. A licensed feed may offer clearer rights and more stable access.

Authentication, paywalls, and explicit technical restrictions require authorization. Stop the job when the approved access path no longer works.

Collect the fields required for the documented purpose and set their retention period in advance. Personal or sensitive data needs a lawful basis and suitable controls.

Use conservative request rates, cache reusable results, and support conditional requests where available. Honor `Retry-After` and reduce traffic when the source signals overload.

Protect credentials in an approved secret manager and restrict access by environment. Rotate exposed secrets, redact logs, and keep certificate checks enabled.

Document the source, purpose, owner, schedule, output fields, retention, and approved access method. Revisit that record after a policy, selector, or business-purpose change.

How Does Proxidize Support Java Web Scraping?

Proxidize gives the Java scraper a managed HTTP proxy endpoint with host, port, and optional credentials. jsoup accepts those settings without another Java dependency.

For a Per GB plan, create an HTTP access point and set `PROXY_HOST` and `PROXY_PORT`. Add the credential variables when that access point uses username/password authentication. The same Java job can then run through the configured route.

Proxidize Residential Proxies fit Java jobs that collect location-specific records across supported countries. Configure country, city, or ISP selection in the access point instead of changing parser code.

Rotating behavior suits independent observations that need route diversity. Sticky sessions suit related requests that must retain one route and cookie jar temporarily.

Proxidize Mobile Proxies fit Java tests that specifically require a mobile-network route. Choose the product from the required network type, geography, session behavior, and current inventory.

Username/password access works with the sample's authentication callback. Source-IP authentication works when the Java host uses an address saved in the allowlist.

Compare complete records, transferred bytes, latency, and failures across the direct and proxied runs. That comparison shows whether the selected route improves the actual workload.

Best For: Residential Proxies fit Java jobs that compare structured records across supported global locations.

Best For: Mobile Proxies fit Java tests that require a mobile-network route in a supported location.

What Should You Remember About Web Scraping With Java?

Web scraping with Java works best when each component has a clear job. Begin with a small jsoup collector, inspect its output, and add infrastructure when the source requires it.

  • Use jsoup for static HTML retrieval and CSS-selector extraction.
  • Add Selenium when JavaScript or browser interaction produces the required values.
  • Set finite limits for pages, redirects, response size, request time, retries, and workers.
  • Choose proxy routes from geography, network type, and session needs.
  • Measure complete records, failure classes, bandwidth, and cost before increasing volume.

Frequently asked questions

Java suits scraping projects that need typed models, mature libraries, controlled concurrency, and integration with existing JVM systems. jsoup handles static pages, while Selenium supports rendered sources and browser interaction. Production reliability also depends on maintainable selectors, source-specific pacing, finite queues, and useful logs.

The best Java library depends on the response format and required interaction. jsoup suits static retrieval and HTML parsing, while Selenium suits rendered pages. Larger crawls may also need JDK `HttpClient`, persistent queues, deduplication, scheduling, and monitoring beyond either library.

jsoup parses returned HTML without running page JavaScript. Inspect the original response, documented endpoints, and browser network calls before choosing Selenium. If execution produces the required values, use a browser and wait for a measurable page condition before extraction begins.

Create a reusable jsoup session, call `proxy()` with the endpoint, and add proxy-only authentication when credentials are required. Keep secrets in environment variables or an approved secret store. HTTPS targets may require `jdk.http.auth.tunneling.disabledSchemes` to allow Basic proxy authentication on CONNECT tunnels.

No universal concurrency value fits every Java scraper or source. Start with one worker, then measure latency, complete results, `429` responses, and challenges. Increase gradually within documented limits while keeping all active queues, retries, connections, and per-host request rates bounded.

Java does not determine whether a scraping workflow is lawful or permitted. The answer depends on the source, data, access method, jurisdiction, contracts, and purpose. Review applicable laws, website terms, robots instructions, privacy duties, and authorization, then seek legal advice for high-risk collection.

Ready to launch?

Proxies built for real operations.

For teams that depend on stability, not luck.