DEV Community

Cover image for How to Use cURL for Web Scraping: A Complete Beginner-to-Practical Guide
IPFoxy
IPFoxy

Posted on

How to Use cURL for Web Scraping: A Complete Beginner-to-Practical Guide

Web scraping is a common way to collect publicly available website data for price monitoring, competitor analysis, SEO research, and market intelligence. Among the many scraping tools available, cURL stands out as a lightweight, flexible command-line utility that supports a wide range of HTTP requests.

With cURL, developers can send HTTP requests to retrieve HTML or JSON, customize request headers, configure proxies, and combine it with Python to collect data at scale.

So how do you scrape websites with cURL? This guide walks through setup, practical examples, common issues, and troubleshooting.

I. What Is cURL and Why Use It for Web Scraping?

cURL is a command-line tool for transferring data using URLs. It supports HTTP, HTTPS, and other network protocols, letting developers send requests, inspect server responses, and save the results locally.

For web scraping, cURL typically retrieves HTML pages or API responses, while other tools handle parsing and storage.

Here are the main benefits of using cURL for web scraping:

  • Lightweight and easy to use: Fetch web content with a simple command, without launching a browser.
  • Flexible request controls: Supports GET, POST, custom headers, cookies, and authentication.
  • Easy to automate: Works with Shell scripts and Python for batch collection.
  • Proxy support: Route requests through HTTP(S) or SOCKS5 proxies to manage network egress.

However, cURL does not execute JavaScript like a browser. For sites that load content dynamically, you may need an available API or browser automation tools such as Playwright or Selenium.

II. Getting Ready for cURL Web Scraping: Installation and Proxy Setup

Before scraping, check your cURL installation and configure your connection as needed. For multi-region collection, price tracking, or ongoing automation, setting up proxies in advance makes it easier to manage outbound connections without repeatedly changing scripts.

1. Check and Install cURL

cURL runs on Windows, macOS, and Linux. Open your terminal or command prompt and enter:

curl --version

If you see version details, cURL is installed. Otherwise, download the appropriate version from the official cURL website.

In Windows PowerShell, use curl.exe --version to avoid alias conflicts in older PowerShell versions.

2. Why Use Proxies with cURL Web Scraping?

A local connection is enough for basic page tests. In real scraping workflows, however, you may need to access public data as shown in different regions or manage outbound traffic across multiple collection jobs.

For example, e-commerce websites may display different prices, currencies, or stock availability depending on location. A proxy in the target region helps test the response from that network location.

For these tasks, IPFoxy rotating residential proxies are an option. IPFoxy offers residential proxy resources across 200+ countries and regions, supports HTTP(S) and SOCKS5, and provides country-, state-, and city-level targeting for public data collection, price monitoring, and market research.

3. How to Configure an IPFoxy Proxy in cURL

Step 1: Get Your Proxy Connection Details

Log in to IPFoxy, select rotating residential proxies, and generate a proxy endpoint. Set your target country, region, and rotation strategy based on your needs, then copy the host, port, username, and password.

Step 2: Configure the Proxy and Test the Connection

Use the -x option to specify a proxy server and -U for proxy authentication.

curl -x http://gate-sg.ipfoxy.io:58688 \
  -U "YOUR_USERNAME:YOUR_PASSWORD" \
  "https://api.ipify.org?format=json"

Replace the placeholder credentials with those generated in your IPFoxy dashboard. If the request returns your outbound IP address, the proxy connection is working.

You can then reuse the same proxy settings in subsequent scraping commands to send requests through the chosen network endpoint.

Remember that proxies help manage network location and outbound traffic. They do not replace website authorization or grant permission to bypass access restrictions.

III. Hands-On cURL Web Scraping: From HTTP Requests to Data Extraction

We'll use Quotes to Scrape, a website built for scraping practice, to demonstrate how to fetch pages, extract content, and export results to CSV.

The Shell commands below work on macOS, Linux, and Bash-compatible terminals. Windows users can run them in WSL or Git Bash.

1. Send a GET Request to Retrieve HTML

GET is the most common HTTP request method and can retrieve a page's HTML directly.

curl https://quotes.toscrape.com/

After running the command, the terminal displays the HTML source, including the page title, text, and markup.

If the site redirects requests, add -L to follow HTTP redirects automatically.

2. Save the Page and Check the Response Status

For real-world collection, it's useful to save HTML locally for later parsing.

curl -L --fail \
  -o page.html \
  -w "%{http_code}\n" \
  https://quotes.toscrape.com/

Here's what each option does:

  • -L: Follow HTTP redirects automatically.
  • --fail: Return an error status for most HTTP responses of 400 or higher.
  • -o: Save the response to a specified file.
  • -w: Print the HTTP status code to help verify the request.

A 200 response usually means the HTTP request succeeded, but you should still check that the returned content is what you expected.

To scrape through IPFoxy, simply add the -x and -U options introduced earlier.

3. Customize Request Headers

Some websites vary their responses based on HTTP headers. cURL lets you add headers with -H and set a User-Agent with -A.

curl -L https://quotes.toscrape.com/ \
  -A "MyScraper/1.0" \
  -H "Accept: text/html"

User-Agent identifies the requesting client, while Accept indicates the desired response format.

For sites requiring authentication, cURL also supports cookies and other authentication options. Make sure you have permission to access the content.

4. Retrieve JSON Data from an API

Beyond HTML pages, cURL can request APIs and retrieve JSON directly.

For example, use this public test API:

curl -s https://jsonplaceholder.typicode.com/posts/1

The server returns JSON containing fields such as the post ID, title, and body.

If jq is installed, you can extract a specific field:

curl -s https://jsonplaceholder.typicode.com/posts/1 | jq -r '.title'

Working with JSON is often more efficient than parsing complex HTML. When a website provides an authorized API, consider using it first.

5. Scrape Multiple Pages and Extract Data

Once you can fetch one page, use a Shell loop to download multiple pages.

For example, download the first three pages of Quotes to Scrape:

for page in 1 2 3
do
  curl -L --fail \
    "https://quotes.toscrape.com/page/$page/" \
    -o "page_$page.html"
  sleep 2
done

This script downloads three pages in sequence, pausing for two seconds between requests. Adjust request frequency to comply with the target site's access rules.

Once the pages are downloaded, use Python and BeautifulSoup to extract quotes and authors.

First, install the parsing library:

pip install beautifulsoup4

Next, create a file named parse.py and enter:

from bs4 import BeautifulSoup
import csv
import glob

with open("quotes.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerow(["Quote", "Author"])

    for filename in sorted(glob.glob("page_*.html")):
        with open(filename, encoding="utf-8") as html:
            soup = BeautifulSoup(html.read(), "html.parser")

        for item in soup.select(".quote"):
            quote = item.select_one(".text").get_text(strip=True)
            author = item.select_one(".author").get_text(strip=True)
            writer.writerow([quote, author])

print

Run python parse.py to read the downloaded HTML files, extract the quotes and author names, and save everything to quotes.csv.

You've now completed the workflow: sending requests with cURL, downloading multiple pages, parsing HTML with Python, and exporting the data to CSV.

IV. Common cURL Web Scraping Issues and How to Fix Them

When scraping with cURL, you may encounter failed requests, timeouts, or incomplete data. Here are five common issues and their solutions.

1. cURL Returns 403 Forbidden

A 403 response means the server understood the request but refused access. Possible causes include insufficient permissions, failed authentication, or website access policies.

Solution: Check whether automated access is allowed and verify your headers, credentials, and permissions. If the website restricts access, respect its rules rather than repeatedly retrying.

2. cURL Returns 429 Too Many Requests

A 429 response indicates that your requests exceeded the server's rate limit, often after fetching too many pages in a short period.

Solution: Reduce request frequency, add reasonable delays, and follow any Retry-After instructions. For ongoing tasks, use exponential backoff to avoid repeated rate-limit errors.

3. cURL Connection Times Out or Fails

An unstable network, slow server, or proxy connectivity issue can cause cURL requests to time out.

Solution: Check the website and your local connection first. Then configure retry and timeout options as needed, such as --retry 3, --connect-timeout 10, and --max-time 30 to control retries, connection timeout, and total request duration.

4. cURL Returns Incomplete HTML

Some sites load product details, reviews, or other page content with JavaScript. Since cURL doesn't execute JavaScript, a successful request may still return incomplete data.

Solution: First check whether the site offers an API you are authorized to use. If the content requires browser rendering, consider Playwright or Selenium.

5. cURL Proxy Connection Fails

After enabling a proxy, connection errors, 407 Proxy Authentication Required, or empty responses may indicate an incorrect host, port, protocol, or credentials.

Solution: Verify that the proxy host, port, username, and password match your IPFoxy dashboard, and check your HTTP or SOCKS5 settings. Test the connection with an IP-check endpoint before troubleshooting the target website.

V. Conclusion

cURL is a lightweight, flexible tool for retrieving HTML, querying JSON APIs, and running straightforward automated data collection tasks. Combined with Shell scripts and Python, it also supports pagination, parsing, and CSV exports.

For public data collection across regions, IPFoxy residential proxies can help you manage outbound network locations. Combine them with sensible request rates, timeouts, and error handling to make the workflow more reliable.

Keep in mind that cURL cannot render JavaScript. For complex dynamic websites, consider pairing it with Playwright or Selenium, depending on your data collection needs.

Top comments (0)