Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautifulsoup - How to open images and download them

I am looking to grab the full size product images from here

My thinking was:

  • Follow the image link
  • Download the picture
  • Go back
  • Repeat for n+1 pictures

I know how to open the image thumbnails but not how to get the full size images. Any ideas on how this could be done?

like image 679
Ninja2k Avatar asked Aug 28 '13 20:08

Ninja2k


People also ask

How do you scrape and download all images from a webpage with Python?

A simple code to perform the download: text, 'html. parser') image_tags = soup. find_all('img') urls = [img['src'] for img in image_tags] for url in urls: filename = re.search(r'/([\w_-]+[.]( jpg|gif|png))$', url) if not filename: print("Regular expression didn't match with the url: {}".


1 Answers

This will get you all URL of the images:

import urllib2
from bs4 import BeautifulSoup

url = "http://icecat.biz/p/toshiba/pscbxe-01t00een/satellite-pro-notebooks-4051528049077-Satellite+Pro+C8501GR-17732197.html"
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)

imgs = soup.findAll("div", {"class":"thumb-pic"})
for img in imgs:
        print img.a['href'].split("imgurl=")[1]

Output:

http://www.toshiba.fr/contents/fr_FR/SERIES_DESCRIPTION/images/g1_satellite-pro-c850.jpg
http://www.toshiba.fr/contents/fr_FR/SERIES_DESCRIPTION/images/g4_satellite-pro-c850.jpg
http://www.toshiba.fr/contents/fr_FR/SERIES_DESCRIPTION/images/g2_satellite-pro-c850.jpg
http://www.toshiba.fr/contents/fr_FR/SERIES_DESCRIPTION/images/g5_satellite-pro-c850.jpg
http://www.toshiba.fr/contents/fr_FR/SERIES_DESCRIPTION/images/g3_satellite-pro-c850.jpg

And this code is for downloading and saving those images:

import os
import urllib
import urllib2
from bs4 import BeautifulSoup

url = "http://icecat.biz/p/toshiba/pscbxe-01t00een/satellite-pro-notebooks-4051528049077-Satellite+Pro+C8501GR-17732197.html"
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)

imgs = soup.findAll("div", {"class":"thumb-pic"})
for img in imgs:
        imgUrl = img.a['href'].split("imgurl=")[1]
        urllib.urlretrieve(imgUrl, os.path.basename(imgUrl))
like image 133
4d4c Avatar answered Nov 15 '22 15:11

4d4c