我对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
我做错什么了?
除了丢失的
import re
,您的程序还有另一个错误。在在行尾的
read
之后,您将()
关闭。所以目前webpage
是对.read
方法的引用,它不是.read()
调用的结果。您需要在代码中导入regular expression module
相关问题 更多 >
编程相关推荐