2

I have this code where i would like to download the image and save it into a folder but i am getting the src of the image.I have gone through stack overflow where i found this Batch downloading text and images from URL with Python / urllib / beautifulsoup? but have no idea how to proceed

Here is my code,so far i have tried

elm5=soup.find('div', id="dv-dp-left-content")
img=elm5.find("img")
src = img["src"]
print src

How can i download these images using url into a folder

Community
  • 1
  • 1
Fazeela Abu Zohra
  • 641
  • 1
  • 12
  • 31
  • 3
    Did you read the source code in the *question*? The `` tag is *just a pointer*, the `src` attribute tells your browser where to load the image. It is not included directly in the HTML itself. – Martijn Pieters Jun 19 '14 at 10:23
  • try this : http://stackoverflow.com/questions/18497840/beautifulsoup-how-to-open-images-and-download-them – Vipul Jun 19 '14 at 10:30

2 Answers2

3

EDIT: 2021.07.19

Updated from urllib (Python 2) to urllib.request (Python 3)


import urllib.request

f = open('local_file_name','wb')
f.write(urllib.request.urlopen(src).read())
f.close()

src have to be full path - for examplehttp://hostname.com/folder1/folder2/filename.ext.

If src is /folder1/folder2/filename.ext you have to add http://hostname.com/.
If src is folder2/filename.ext you have to add http://hostname.com/folder1/.
etc.


EDIT: example how to download StackOverflow logo :)

import urllib.request

f = open('stackoverflow.png','wb')
f.write(urllib.request.urlopen('https://cdn.sstatic.net/Img/unified/sprites.svg?v=fcc0ea44ba27').read())
f.close()
furas
  • 134,197
  • 12
  • 106
  • 148
1

the src attribute contains the image's url.

you can download it with:

urllib.request.urlretrieve(src, "image.jpg")

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143