【发布时间】:2011-09-06 03:27:03
【问题描述】:
我使用的是 Python 2.7。当我尝试运行此代码时,当函数命中 print findPatTitle[i] 时出现问题,并且 python 返回“索引错误:列表索引超出范围”。我从 youtube 上的第 13 个 python 教程中获取这段代码,我很确定代码是相同的,所以我不明白为什么我会遇到范围问题。有什么想法吗?
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"
【问题讨论】:
-
当你有 BeautifulSoup 时,为什么要使用正则表达式来解析 html? o.O 你不应该用正则表达式解析 html...stackoverflow.com/questions/590747/…
标签: python beautifulsoup web-scraping