I am writing a project from the Automate The Boring Stuff book. The task is the following:
Image Site Downloader
Write a program that goes to a photo-sharing site like Flickr or Imgur, searches for a category of photos, and then downloads all the resulting images. You could write a program that works with any photo site that has a search feature.
Here is my code:
import requests, bs4, os
# The outerHTML file which I got by rightClicking and copying the <html> tag on 'page source'
flickrFile=open('flickrHtml.html',encoding="utf8")
#Parsing the HTML document
flickrSoup=bs4.BeautifulSoup(flickrFile,'html.parser')
# categoryElem is the Element which has image source inside
categoryElem=flickrSoup.select("a[class='overlay']")
#len(categoryElem)=849
os.makedirs('FlickrImages', exist_ok=True)
for i in range(len(categoryElem)-1):
# Regex searching for the href
import re
html=str(categoryElem[i])
htmlRegex=re.compile(r'href.*/"')
mo=htmlRegex.search(html)
imageUrl=mo.group()
imageUrl=imageUrl.replace('"','')
imageUrl=imageUrl.replace('href=','')
imageUrlFlickr="https://www.flickr.com"+str(imageUrl)
# Downloading the response object of the Image URL
res = requests.get(imageUrlFlickr)
imageSoup=bs4.BeautifulSoup(res.text)
picElem=imageSoup.select('div[class="view photo-well-media-scrappy-view requiredToShowOnServer"] img')
# Regex searching for the jpg file in the picElem HTML element
html=str(picElem)
htmlRegex=re.compile(r'//live.*\.jpg')
mo=htmlRegex.search(html)
try:
imageUrlRegex=mo.group()
except Exception as exc:
print('There was a problem: %s' % (exc))
res1=requests.get('https:'+imageUrlRegex)
try:
res1.raise_for_status()
except Exception as exc:
print('There was a problem: %s' % (exc))
# Dowloading the jpg to my folder
imageFile = open(os.path.join('FlickrImages', os.path.basename(imageUrlRegex)), 'wb')
for chunk in res1.iter_content(100000):
imageFile.write(chunk)
After looking up this question, I figured that for downloading all of the 4 million results for picture "Sea", I copy (as said in the answer to the question stated) the whole OuterHTML. If I would have not looked at this question, and not have copied the full HTML source (in my code, it's stored in flickrFile=open('flickrHtml.html',encoding="utf8")), I would end up having categoryElem equal to 24, and hence downloading only 24 pictures, instead of 849 pics.
There are 4 million pictures, how do I download all of them, without having to download the HTML source to a separate file?
I was thinking of my program to do the following:
- Getting the url of the first picture of the search --> downloading the picture --> getting the next picture's url --> downloading the pic.... and so on until there is nothing left to download.
I did not go with the first approach because I did not know how to get to the link of the first picture. I tried getting the URL of it, but then when I inspect the element of the first pic (or any other pic) from the "photo stream", it gives me a link to the "photo stream" of the specific user, not the general "Sea Search photo stream".
Here is the link for the photo stream Search
If someone could help me with that too, it would be fantastic.
Here is some code from someone who did the same task, but he's downloading only the first 24 pictures, which are the pictures that show up on the original, un-rendered HTML
