I use the following code:
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
And I get the following error:
SyntaxError: 'continue' not properly in loop.
Why is that? I have checked indentations and colons. Thanks for your help in advance.
continuekeyword should be inside a loop. What do you want it to do here ?continueis a smell for me unless there's a convincing reason; could you share what the exception you're guarding against would be?print names,years etcis inside thatexcept, if the program runs here, that means sth's wrong in yourtry, and some of the variables (names,yearsetc ) must be wrong,too.Thus you cannot print them out. Hope I was understood :)