Reputation: 149
I am getting the correct url for the image. But I can't seem to be able to download the image and save it to a file. I am new to python so any guidance would be greatly appreciated. I have tried this with several other article sources and haven't had any trouble downloading the image once I get the url. Guess it doesn't like africom?
url: http://www.africom.mil/Newsroom/Article/12058/multinational-participation-plays-key-factor-to-exercise-african-lion
soup = BeautifulSoup(urllib2.urlopen(url).read())
links = soup.find("div", {'class': 'usafricom_ArticlePhotoContainer'}).find_all('img', src=True)
for link in links:
imgfile = open('%s' % timestamp + "_" + title.encode("utf-8") + ".jpg", "wb")
link = link["src"].split("src=")[-1]
imgurl = "www.africom.mil" + link + ".jpg"
download_img = urllib2.urlopen(imgurl).read()
imgfile.write(download_img)
imgfile.close()
Upvotes: 0
Views: 192
Reputation: 12092
I am not sure what is the error you are seeing with your code. Your question does not mention the error. When I tried your code, I hit this error:
ValueError: unknown url type: www.africom.mil/Image/12059/High/030414-M-XI134-002.jpg
This error is because of this line in your code:
imgurl = "www.africom.mil" + link + ".jpg"
It does not specify the http protocol. Change it to:
imgurl = "http://www.africom.mil" + link + ".jpg"
and check. It worked for me with this change.
Upvotes: 1