@uncollectedはおそらくコメントであなたに正しい答えを持っていましたが、私はそれを拡張したいと思いました。
正確なコードを呼び出しているが、while
ブロックにネストされている場合は、最初の結果ですぐに返されます。ここでは2つのことができます。
while
自分のコンテキストでをどのように使用しているかわからないため、for
ここではループを使用しています。
結果リストを拡張し、リスト全体を返します
def getLinks(urls):
""" processes all urls, and then returns all links """
links = []
for givenurl in urls:
page = urllib2.urlopen(givenurl,"",10000)
soup = BeautifulSoup(page, "lxml")
linktags = soup.findAll('span',attrs={'class':'paginationLink pageNum'})
page.close()
BeautifulSoup.clear(soup)
links.extend(linktags)
# dont return here or the loop is over
return links
または、戻る代わりに、キーワードを使用してジェネレーターにyield
することができます。ジェネレータは各結果を返し、次のループまで一時停止します。
def getLinks(urls):
""" generator yields links from one url at a time """
for givenurl in urls:
page = urllib2.urlopen(givenurl,"",10000)
soup = BeautifulSoup(page, "lxml")
linktags = soup.findAll('span',attrs={'class':'paginationLink pageNum'})
page.close()
BeautifulSoup.clear(soup)
# this will return the current results,
# and pause the state, until the the next
# iteration is requested
yield linktags