My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
webCrawler  
Cliente web nivel 2: Un web Crawler sencillo para ver los enlaces internos de un dominio.
webcrawler
Updated Aug 12, 2009 by ecasbas

Introducción

Web crawler sencillo de nivel 2. Para una URL dada mostrará todos los enlaces internos.

Ejemplo:

ecasbas@server:~/crawlers$ python simplecrawl.py http://www.google.es
http://www.google.es/intl/es/about.html
http://www.google.es/intl/es/ads/
http://www.google.es/intl/es/privacy.html
http://www.google.es/search?q=Perseidas&hl=es&ct=perseids09&oi=ddle
http://www.google.es/services/
http://groups.google.es/grphp?hl=es&tab=wg
http://images.google.es/imghp?hl=es&tab=wi
http://mail.google.com/mail/?hl=es&tab=wm
http://maps.google.es/maps?hl=es&tab=wl
http://news.google.es/nwshp?hl=es&tab=wn
http://video.google.es/?hl=es&tab=wv
http://www.google.es/intl/es/options/
http://www.google.es/setprefs?sig=0_3Bdo_-q-4Ru_cObjajpdclHS9IA=&hl=ca
http://www.google.es/setprefs?sig=0_3Bdo_-q-4Ru_cObjajpdclHS9IA=&hl=eu
http://www.google.es/setprefs?sig=0_3Bdo_-q-4Ru_cObjajpdclHS9IA=&hl=gl
https://www.google.com/accounts/Login?hl=es&continue=http://www.google.es/

CODIGO

import urllib2
import re
import sys
from operator import itemgetter
from itertools import groupby

linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')
#igual para script src

if len(sys.argv) < 2:
        print "Dominio no especificado"
        sys.exit()

# [1:] lo convierte en lista
url = sys.argv[1]

f = urllib2.urlopen(url)

msg = f.read()

links = linkregex.findall(msg)

#ordena y elimina repetidos
links.sort()
unique_list = list(map(itemgetter(0), groupby(links)))
#print unique_list

for link in xrange(len(unique_list)):
        if unique_list[link].startswith('http://') or unique_list[link].startswith('https://'):
                print unique_list[link]
        else:
                linknew = url + '/' + unique_list[link]
                print linknew

f.close()

Add your content here. Format your content with:

  • Text in bold or italic
  • Headings, paragraphs, and lists
  • Automatic links to other wiki pages

Comment by LaTeXFig...@gmail.com, Sep 13, 2009

import urllib2 import re import sys from operator import itemgetter from itertools import groupby linkregex = re.compile('<a\s*href=[\'|"](.?)[\'"].?>') #igual para script src if len(sys.argv) < 2:

print "Dominio no especificado" sys.exit()
# [1:] lo convierte en lista url = sys.argv1? f = urllib2.urlopen(url) msg = f.read() links = linkregex.findall(msg) #ordena y elimina repetidos links.sort() unique_list = list(map(itemgetter(0), groupby(links))) #print unique_list for link in xrange(len(unique_list)):
if unique_listlink?.startswith('http://') or unique_listlink?.startswith('https://'):
print unique_listlink?
else:
linknew = url + '/' + unique_listlink? print linknew
f.close()


Sign in to add a comment
Powered by Google Project Hosting