NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I download images and files while scraping?

Collect the file URLs from the page, turn them into absolute addresses, and stream each one to disk with the same HTTP session that loaded the page.

The short version

Find the URL in the HTML (an img src, a srcset entry, or an a href pointing at a PDF), resolve it against the page address with urljoin, then download it with streaming so large files do not sit in memory. Reuse the session that fetched the page, because some servers only serve files to clients that carry the page's cookies and a matching Referer.

A working Python example

import hashlib, pathlib
import urllib.parse
import requests, bs4

out_dir = pathlib.Path("downloads"); out_dir.mkdir(exist_ok=True)
grabber = requests.Session()
grabber.headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) catalog-archiver"

page_url = "https://catalog.example.com/item/4411"
soup = bs4.BeautifulSoup(grabber.get(page_url, timeout=20).text, "html.parser")
for img in soup.select("div.gallery img[src]"):
    file_url = urllib.parse.urljoin(page_url, img["src"])
    name = hashlib.sha1(file_url.encode()).hexdigest()[:16]
    target = out_dir / (name + pathlib.Path(file_url).suffix.split("?")[0])
    if target.exists():
        continue
    with grabber.get(file_url, stream=True, timeout=60, headers={"Referer": page_url}) as r:
        r.raise_for_status()
        with open(target, "wb") as fh:
            for chunk in r.iter_content(64 * 1024):
                fh.write(chunk)

Hashing the URL into the file name means a second run skips files it already saved, which matters when you scrape on a schedule.

Getting the right URL

  • Lazy loading. The real address often sits in data-src, data-original or similar, while src holds a placeholder. Check both.
  • Responsive images. srcset lists several widths. Split it on commas and take the largest; the question on full size image URLs covers common URL patterns for thumbnails.
  • Background images. Some galleries set images in CSS background-image: url(...). Read the style attribute with a regular expression.
  • Script data. Product galleries often live in a JSON block, which lists every image even when the page shows only the first.

Checking what you received

A download can succeed with the wrong content: an HTML error page saved as photo.jpg is a classic. Check the Content-Type header and the first bytes before keeping the file. JPEG starts with FF D8, PNG with 89 50 4E 47, PDF with %PDF. Discard anything that does not match and log the URL.

Being a polite downloader

Files are heavier than pages, so they put more load on the target. Download with a small pool of workers, a pause between requests, and only the sizes you need. Respect robots.txt rules for image paths and the site's terms, and make sure you have the right to store and use the files: product photos and documents are usually copyrighted even when they are public. When a site serves images from a CDN, the CDN may have its own rate limits, so watch for 429 replies there too.

Files behind a check

Some download links sit behind a CAPTCHA, typically a reCAPTCHA or Turnstile on a "download" form. Solve the widget, submit the form with the token in the same session, and follow the redirect to the file. If the site shows a full challenge page before any file, the clearance cookie from a challenge task, used from the same proxy and user agent, opens the downloads for that session. With a browser such as Puppeteer, the flow differs; see downloading files with Puppeteer.

Storing at scale

Thousands of files in one folder slow down most file systems. Split by the first characters of the hash, keep a small index (URL, hash, size, date) in a database, and upload to object storage if several machines need the files. The same index lets you detect when a site replaces an image under the same URL.

Common questions

Why are my downloaded images only a few hundred bytes?

You probably saved a placeholder or an error page. Check data-src attributes and the Content-Type of the reply.

Should I use the same session for images and pages?

Yes. Cookies and headers from the page load are often required to fetch protected files.

How do I avoid downloading the same file twice?

Name files by a hash of the URL or content and skip existing names, or keep an index of saved URLs.

Can I download in parallel?

Yes, with a small worker pool and a pause between requests so you do not overload the host.

Is a public image free to reuse?

Not necessarily. Copyright still applies, so check the site's terms and your intended use.

Keep reading