Tema: Crawler
Ver Mensaje Individual
  #20 (permalink)  
Antiguo 21/02/2011, 14:22
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Crawler

Código Python:
Ver original
  1. import urllib
  2. import os
  3. from BeautifulSoup import BeautifulSoup
  4.  
  5. url = raw_input('URL -> ')
  6.  
  7. f = urllib.urlopen(url)
  8. data = f.read()
  9. f.close()
  10.  
  11. soup = BeautifulSoup(data)
  12. for counter, img in enumerate(soup.findAll('img')):
  13.     if img['src'].startswith("http://"):
  14.         ipath = img['src']
  15.     else:
  16.         ipath = url + img['src']
  17.  
  18.     try:
  19.         local = open(img['src'].split("/")[-1]), "wb")
  20.         internet = urllib.urlopen(ipath)
  21.     except IOError:
  22.         continue
  23.  
  24.     data = internet.read()
  25.     local.write(data)
  26.  
  27.     internet.close()
  28.     local.close()
Mas o menos quedaría así.