2011-09-19 16 views
12

Soy muy nuevo en python. Muy nuevo. He copiado el siguiente de un tutorialNameError: name 're' no está definido

#!/usr/bin/python 
import re 
from urllib import urlopen 
from BeautifulSoup import BeautifulSoup 

webpage = urlopen('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read 

patFinderTitle = re.compile('<title>(.*)</title>') 

patFinderLink = re.compile('<link rel.*href="(.*)"/>') 

findPatTitle = re.findall(patFinderTitle,webpage) 

findPatLink = re.findall(patFinderLink,webpage) 

listIterator = [] 
listIterator[:] = range(2,16) 

for i in listIterator: 
    print findPatTitle[i] 
    print findPatLink[i] 
    print "\n" 

me sale el error:

Traceback (most recent call last): 
    File "test.py", line 8, in <module> 
    patFinderTitle = re.compile('<title>(.*)</title>') 
NameError: name 're' is not defined 

¿Qué estoy haciendo mal?

Edición: He añadido import re pero ahora sale el siguiente:

File "/scripts/_prod/test.py", line 13, in <module> 
    findPatTitle = re.findall(patFinderTitle,webpage) 
    File "/usr/lib64/python2.6/re.py", line 177, in findall 
    return _compile(pattern, flags).findall(string) 
TypeError: expected string or buffer 
+0

¿De qué tutorial copió esto? Está plagado de errores. – Johnsyweb

+0

http://www.youtube.com/watch?v=Ap_DlSrT-iE&feature=related –

+0

Luego debe comparar su código con el código que lo acompaña aquí: http://www.newthinktank.com/2010/11/python-2 -7-tutorial-pt-13-web-scraping /. Después de un poco de limpieza, descubrí que funciona. – Johnsyweb

Respuesta

19

necesita importar regular expression module en el código

import re 
re.compile('<title>(.*)</title>') 
+0

gracias. Ahora obtengo otro error ... vea mis ediciones –

+3

@ user522962 'webpage = urlopen ('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews') .read' debe ser' webpage = urlopen ('http: // feeds.huffingtonpost.com/huffingtonpost/LatestNews '). read() ' – razpeitia

1

Actualmente webpage es una referencia a una función. Sospecho que dejó el () apagado después de read

Cuestiones relacionadas