diff options
Diffstat (limited to 'Chapter11PracticeImageSiteDownload.py')
-rw-r--r-- | Chapter11PracticeImageSiteDownload.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Chapter11PracticeImageSiteDownload.py b/Chapter11PracticeImageSiteDownload.py new file mode 100644 index 0000000..3a1a3ba --- /dev/null +++ b/Chapter11PracticeImageSiteDownload.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +#Chapter 11 Practice +#Image Site Downloader - Downloads images in an Imgur catagory +#Change URL variable to the Imgur catagory link + +import requests +import os +import bs4 + +url = 'http://imgur.com/topic/The_Great_Outdoors' +os.makedirs('imgur', exist_ok=True) + +print('Downloading page %s...' % url) +res = requests.get(url) +res.raise_for_status() + +soup = bs4.BeautifulSoup(res.text) +postElem = soup.select('a img') +if postElem == []: + print('Could not find the posts images.') +else: + for i in range(0, len(postElem)): + postUrl = 'http:' + postElem[i].get('src') + print('Downloading image %s...' % (postUrl)) + res = requests.get(postUrl) + res.raise_for_status() + imageFile = open(os.path.join('imgur', os.path.basename(postUrl)), 'wb') + for chunk in res.iter_content(1000000): + imageFile.write(chunk) + imageFile.close() + +print('Done.') |