Python Script to Scrape and Download Images from Divnil.com
This article explains how to use Python's requests and BeautifulSoup libraries to crawl Divnil.com, extract image detail page URLs, parse each page for high‑resolution image links, and download the images into a local folder with a complete, runnable script.
This guide shows how to automatically download wallpapers from Divnil.com using Python.
Step 1: Install required libraries
<code>pip install requests bs4</code>Step 2: Import modules
<code>import requests
from bs4 import BeautifulSoup
import sys
import os</code>Step 3: Request the main page and collect detail‑page links
<code>url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99_2.html"
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"}
resp = requests.get(url, headers=headers)
if resp.status_code != requests.codes.OK:
print("Request Error, Code: %d" % resp.status_code)
sys.exit()
soup = BeautifulSoup(resp.text, "html.parser")
contents = soup.find("div", id="contents")[0]
wallpapers = contents.findAll("a", rel="wallpaper")
links = []
for wallpaper in wallpapers:
links.append(wallpaper['href'])</code>Step 4: Create a folder for the images
<code>head = "https://divnil.com/wallpaper/iphone8/"
if not os.path.exists("./Divnil"):
os.mkdir("./Divnil")</code>Step 5: Visit each detail page, locate the high‑resolution image, and download it
<code>for url in links:
url = head + url
resp = requests.get(url, headers=headers)
if resp.status_code != requests.codes.OK:
print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
continue
soup = BeautifulSoup(resp.text, "html.parser")
img = soup.find("div", id="contents").find("img", id="main_content")
img_url = head + img['original'].replace("../", "")
img_name = img['alt']
print("start download %s ..." % img_url)
img_resp = requests.get(img_url, headers=headers)
if img_resp.status_code != requests.codes.OK:
print("IMAGE %s DOWNLOAD FAILED." % img_name)
continue
if '/' in img_name:
img_name = img_name.split('/')[-1]
with open("./Divnil/" + img_name + ".jpg", "wb") as f:
f.write(img_resp.content)</code>Step 6: Full script encapsulated in a class
<code>import requests
from bs4 import BeautifulSoup
import sys
import os
class Divnil:
def __init__(self):
self.url = "https://divnil.com/wallpaper/iphone8/%E3%82%A2%E3%83%8B%E3%83%A1%E3%81%AE%E5%A3%81%E7%B4%99.html"
self.head = "https://divnil.com/wallpaper/iphone8/"
self.headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"}
def getImageInfoUrl(self):
resp = requests.get(self.url, headers=self.headers)
if resp.status_code != requests.codes.OK:
print("Request Error, Code: %d" % resp.status_code)
sys.exit()
soup = BeautifulSoup(resp.text, "html.parser")
contents = soup.find("div", id="contents")
wallpapers = contents.findAll("a", rel="wallpaper")
self.links = []
for wallpaper in wallpapers:
self.links.append(wallpaper['href'])
def downloadImage(self):
if not os.path.exists("./Divnil"):
os.mkdir("./Divnil")
for url in self.links:
url = self.head + url
resp = requests.get(url, headers=self.headers)
if resp.status_code != requests.codes.OK:
print("URL: %s REQUESTS ERROR. CODE: %d" % (url, resp.status_code))
continue
soup = BeautifulSoup(resp.text, "html.parser")
img = soup.find("div", id="contents").find("img", id="main_content")
img_url = self.head + img['original'].replace("../", "")
img_name = img['alt']
print("start download %s ..." % img_url)
img_resp = requests.get(img_url, headers=self.headers)
if img_resp.status_code != requests.codes.OK:
print("IMAGE %s DOWNLOAD FAILED." % img_name)
continue
if '/' in img_name:
img_name = img_name.split('/')[-1]
with open("./Divnil/" + img_name + ".jpg", "wb") as f:
f.write(img_resp.content)
def main(self):
self.getImageInfoUrl()
self.downloadImage()
if __name__ == "__main__":
divnil = Divnil()
divnil.main()</code>Running this script will create a Divnil directory and save all discovered high‑resolution wallpapers there.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.