0
我正在使用Python 2.7。当我尝试运行此代码时,当函数命中打印findPatTitle [i]时,出现问题,并且python返回“索引错误:列表索引超出范围”。我从YouTube上的第13个python教程中获取这些代码,并且我很确定代码是相同的,所以我不明白为什么我会遇到范围问题。有任何想法吗?在Python中运行基本Web刮时出现索引错误
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re
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"
为什么你使用正则表达式来解析HTML,当你有BeautifulSoup? o.O你不应该用正则表达式解析html ... http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – naeg