Created
August 6, 2017 21:55
-
-
Save fuzzy-focus/972ceb02e8f78d07b9bc495d1338ca26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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