Scraping Images Program

This program is a template for scraping images.

Usage

Specify the

  1. URL
  2. CSS_SELECTOR for images
  3. HTML tag attribute of those images

Program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import requests
from bs4 import BeautifulSoup

URL = "https://www.ABC.com/"
CSS_SELECTOR = "#TARGET_ID"
IMAGE_URL = "src"

res = requests.get(URL)
bsoup = BeautifulSoup(res.text, "html.parser")
tags = bsoup.select(CSS_SELECTOR)

for i in range(len(tags)):
with open(str(i) + ".jpg", "wb") as f:
doc = requests.get(tags[i].get(IMAGE_URL))
f.write(doc.content)