Guest User

Untitled

a guest
Oct 14th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #! python3
  2. # owahhPics.py - Downloads images from the website.
  3.  
  4. import requests, os, bs4
  5.  
  6. url = 'http://owaahh.com/exploreturkana-photo-essay'
  7. os.makedirs('owaahTurkana', exist_ok= True)
  8.  
  9. while url.startswith('http://owaahh.com'): # deleted the not
  10. # TODO: Download the page.
  11. print('Downloading page %s...' % url)
  12. res= requests.get(url)
  13. res.raise_for_status()
  14.  
  15. soup = bs4.BeautifulSoup(res.text, 'html5lib')
  16. # TODO: Find the URL of the blog image.
  17. picElem = soup.select('.entry-content img')
  18. if picElem == []:
  19. print('Could not find image.')
  20.  
  21. else:
  22. linksHere = len(picElem)
  23. for i in range(linksHere):
  24. picUrl = picElem[i].get('src')
  25. # Download the image.
  26. print('Downloading image %s...' % (picUrl))
  27. #picUrl = picElem[i]
  28. res = requests.get(picUrl)
  29. res.raise_for_status()
  30.  
  31. # TODO: Save the image to ./xkcd.
  32. imageFile = open(os.path.join('owaahTurkana', os.path.basename(picUrl)), 'wb')
  33. for chunk in res.iter_content(100000):
  34. imageFile.write(chunk)
  35. imageFile.close()
  36. print('Done.')
Advertisement
Add Comment
Please, Sign In to add comment