Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active January 18, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqqe/b0f4b24649f62154bc9f307cd867842e to your computer and use it in GitHub Desktop.
Save noqqe/b0f4b24649f62154bc9f307cd867842e to your computer and use it in GitHub Desktop.
Fetches images from http://gatherer.magic.com and saves them to local disk
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from bs4 import BeautifulSoup
import requests
import urllib
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('url', type=str)
ap.add_argument('dir', type=str, default="art")
args = ap.parse_args()
url = "http://gatherer.wizards.com/Pages/Search/Default.aspx?action=advanced&color=+[U]"
def get_no_of_pages(url):
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
for x in soup.find_all('div', {"class", "paging"}):
for a in x.find_all('a'):
lastpage = a["href"]
lastpageid = lastpage.split('=')[1].split("&")[0]
return int(lastpageid)
def fetch_art_from_page(url, dirname):
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
for r in soup.find_all('td', {"class", "leftCol"}):
x = r.find('a')
h = x["href"]
# check if interesting
if not "multiverseid" in h:
continue
# check if cardname is there
try:
title = x.find('img')["alt"].replace("/", "")
except:
continue
cardid = h.split('=')[1]
filename = dirname + '/' + title + '.jpg'
arturl = 'http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=' + cardid + '&type=card'
print("Downloading %s..." % title)
urllib.urlretrieve(arturl, filename)
def main(args=args):
if not os.path.isdir(args.dir):
return False
page = 0
pages = get_no_of_pages(args.url)
while page <= pages:
print("Fetching page %s of %s" % (page, pages))
fetch_art_from_page(args.url + '&page=' + str(page), args.dir)
page += 1
if __name__ == '__main__':
main(args)

Install

pip3.9 install BeautifulSoup4

Usage

for example download all lands

mkdir lands
./archivist.py 'http://gatherer.wizards.com/Pages/Search/Default.aspx?type=+[Land]' lands/

or download all green cards from Urza's Saga

mkdir urzas-saga-green
./archivist.py http://gatherer.wizards.com/Pages/Search/Default.aspx?color=%7C[G]&set=[%22Urza's%20Saga%22] urzas-saga-green/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment