我知道KeyErrors在BeautifulSoup中相当普遍,在你向我大喊RTFM之前,我已经在
Python文档和BeautifulSoup文档中做了大量的阅读.现在,除此之外,我仍然不知道KeyErrors正在发生什么.
这是我正在尝试运行的程序,它始终如一地导致URL列表的最后一个元素出现KeyError.
我来自C背景,只是为了让你知道,但我需要使用BeautifulSoup工作,在C中这样做是一个可以想象的噩梦!
我们的想法是返回网站中所有网址的列表,这些网址在其网页上包含指向某个网址的链接.
这是我到目前为止所得到的:
import urllib
from BeautifulSoup import BeautifulSoup
URLs = []
Locations = []
URLs.append("http://www.tuftsalumni.org")
def print_links (link):
if (link.startswith('/') or link.startswith('http://www.tuftsalumni')):
if (link.startswith('/')):
link = "STARTING_WEBSITE" + link
print (link)
htmlSource = urllib.urlopen(link).read(200000)
soup = BeautifulSoup(htmlSource)
for item in soup.fetch('a'):
if (item['href'].startswith(