Trick 2: Bulk download 4K wallpapers from alphacoders.com

A year ago I had posted about how you can bulk download 1080p wallpapers from Microsoft’s website. You can read the post here. That tricked worked very well and allowed me to download a total of 543 1080p wallpapers in one go. Now, I am using the same concept to download wallpapers from alphacoders.com albeit this time it’s 4K, baby! (3840×2160)

Since the level of our pixel gorgeousness has increased we will step up our game a bit by using a Python script to automate the download. Actually, that’s not the main reason… While using lynx, grep and wget worked previously, it may be a bit untidy here. We are dealing with 117 pages full of 4K wallpapers. I estimate around 3000+ wallpapers here. That’s >6 times as previously.

Using Python requests, BeautifulSoup and shutil, it’s very easy. Here’s how I did it.

DISCLAIMER: The code here is only for educational purposes. Use it at your own risk. I am thankful to the original posters of these 4K wallpapers. They deserve due credit.
1. Construct the page URL that shows thumbnails for 4K wallpapers
The first page is this: http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160
Page 2 is this: http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160&page=2
…you get the pattern, right?

url = 'http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160'
if page_no > 1:
    url += '&page=' + str(page_no)

2.  On the page, look for image tags with ‘alt’ attribute starting with ‘HD Wallpaper’

page = BeautifulSoup(requests.get(url).text)
thumbs_on_this_page = page.find_all('img', attrs={'alt':re.compile('HD Wallpaper')})

3. Loop through all the thumbnails, extract their ‘src’ values and strip ‘thumb-350-‘ string from the ‘src’ values to get the actual 4K wallpaper URL
Input = http://images.alphacoders.com/488/thumb-350-488146.jpg
Output = http://images.alphacoders.com/488/488146.jpg

for thumb in thumbs_on_this_page:
    img = thumb.attrs['src'].replace('thumb-350-','')

4. Download the image and store in a file

r = requests.get(img, stream=True)
if r.status_code == 200:
    with open(file_name, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)

5. Loop through all the pages

for page_no in range (1, TOTAL_PAGES+1):

Complete snippet:

from bs4 import BeautifulSoup
import os
import re
import shutil
import requests

TOTAL_PAGES = 117
DOWNLOAD_DIR = './images'

for page_no in range (1, TOTAL_PAGES+1):
    url = 'http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160'
    if page_no > 1:
        url += '&page=' + str(page_no)

    page = BeautifulSoup(requests.get(url).text)
    thumbs_on_this_page = page.find_all('img', attrs={'alt':re.compile('HD Wallpaper')})
    for thumb in thumbs_on_this_page:
        img = thumb.attrs['src'].replace('thumb-350-','')
        file_name = DOWNLOAD_DIR + '/' + os.path.basename(img)
        if not os.path.isfile(file_name):
            r = jrequests.get(img, stream=True)
            if r.status_code == 200:
                with open(file_name, 'wb') as f:
                    r.raw.decode_content = True
                    shutil.copyfileobj(r.raw, f)

That’s it!

UPDATE:

Here are stats for my download process. Obviously speed stinks but I was able to 99.9% of 4K Wallpapers.

Downloaded: 3501 Skipped: 5
Total size: 8.66gb Speed: 370.86kb/s Elapsed time:  6h 48m 18.7s

7 thoughts on “Trick 2: Bulk download 4K wallpapers from alphacoders.com

  1. Thanks for the snippet , but I get

    ...
     NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection 
    object at 0x0000000003F08898>: Failed to establish a new connection:
    [Errno 10061] No connection could be made because the target machine actively refused it',))
  2. What you have shown is only for geeks. If you cannot program, there is a dedicated tool called wfdownloader app which can do bulk downloads from alphacoders website.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.