Skip to content

Instantly share code, notes, and snippets.

@fuzzy-focus
Created August 6, 2017 21:55
Show Gist options
  • Save fuzzy-focus/972ceb02e8f78d07b9bc495d1338ca26 to your computer and use it in GitHub Desktop.
Save fuzzy-focus/972ceb02e8f78d07b9bc495d1338ca26 to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
import shutil
DIR = '/home/dani/bin/dota_2_items/'
html = requests.get('https://dota2.gamepedia.com/Items').text
s = BeautifulSoup(html, 'html.parser')
mark = s.find('span', id="Items")
imgs = []
for n in mark.parent.next_siblings:
if n.name == 'h2':
break
elif n.name == 'div' and 'itemlist' in n.attrs['class']:
for img in n.find_all('img'):
imgs.append(img)
def rem_price(item):
if '(' in item['alt']:
words = item['alt'].split(' ')
item['alt'] = ' '.join(words[:-1])
return item
imgs = [rem_price(item) for item in imgs]
d = [(item['alt'], 'https://dota2.gamepedia.com/' + item['src']) for item in imgs]
for item in d:
r = requests.get(item[1], stream=True)
if r.status_code == 200:
with open(DIR+item[0]+'.png', 'wb+') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
else: print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment