我使用下面的代碼:的Python:繼續循環
from bs4 import BeautifulSoup
soup = BeautifulSoup (open("43rd-congress.htm"))
final_link = soup.p.a
final_link.decompose()
trs = soup.find_all('tr')
for tr in trs:
for link in tr.find_all('a'):
fulllink = link.get ('href')
print fulllink #print in terminal to verify results
tds = tr.find_all("td")
try:
names = str(tds[0].get_text())
years = str(tds[1].get_text())
positions = str(tds[2].get_text())
parties = str(tds[3].get_text())
states = str(tds[4].get_text())
congress = tds[5].get_text()
except:
print "bad tr string"
continue
print names, years, positions, parties, states, congress
而且我得到以下錯誤:
SyntaxError: 'continue' not properly in loop.
這是爲什麼?我檢查了縮進和冒號。感謝您的幫助提前。
您的代碼的縮進看起來不正確。你在混合製表符和空格嗎? – Blender
'繼續'關鍵字應該在循環中。你想在這裏做什麼? – yakiang
@yakiang我想打印出它的名字,年份,職位,政黨,州議會。「但是,當我刪除繼續時,我收到28行上的打印錯誤。 這有道理嗎? 我正在使用本教程... http://jeriwieringa.com/blog/2012/11/04/beautiful-soup-tutorial-part-1/ –