NameError:未定义名称“re”

时间:2011-09-19 01:17:00

标签: python

我是python的新手。很新。我从教程中复制了以下内容

#!/usr/bin/python

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"

我收到错误:

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

我做错了什么?

2 个答案:

答案 0 :(得分:22)

您需要在代码中导入regular expression module

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

答案 1 :(得分:2)

除了缺少import re之外,您的程序还有另一个错误。在

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

您在()之后离开了read。因此,目前webpage是对.read方法的引用,它不是.read()调用的结果。