【发布时间】:2013-12-30 01:48:21
【问题描述】:
(使用 python 3.3.2)您好,我正在尝试为文本云创建一个爬行函数,该函数将进入链接列表,并理想地返回该列表中每个元素的函数输出列表。但是,我坚持使用打印功能 print(b),而不是真正返回我想要的。在我的 for 循环中,我将如何返回从我的 print(b) 语句中获得的所有内容。它可以全部在一个列表中,也可以以某种方式编译。谢谢 :) tl;dr:如何返回从 for 循环中获得的所有内容
def crawl():
linkList = inputFunction()[1:][0] #makes a list of a bunch of URL's
for i in range(len(linkList)):
print(i)
t = getHTML(linkList[i]) #getHTML returns tuple of text in the input URL
alreadyCrawl = alreadyCrawl + list(linkList[i]) #ignore this
t = list(t)
b = counting(t) #makes dictionary of word counts
print(b)
return
【问题讨论】:
-
还可以考虑使用
for link in linkList而不是for i in range(len(linkList)): ... linkList[i]。 -
通常值得避免使用
for i in range(len(linkList)):之类的东西——它可以更清楚地写成for i, item in enumerate(linkList):(如果你不再需要实际使用i,则写成for item in linkList:) .
标签: python loops for-loop return python-3.3