【发布时间】:2013-10-26 01:04:05
【问题描述】:
我正在使用 BeautifulSoup,但我不断收到错误 continue not proper in loop。所以我删除了继续,然后我的打印语句出现无效的语法错误。我正在运行 BS4 和 Python 2.7.5,非常感谢所有帮助。这是我的代码。
from bs4 import BeautifulSoup
soup = BeautifulSoup (open("43rd-congress.html"))
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: #we are using "try" because the table is not well formatted.
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
【问题讨论】:
-
您希望
continue在这里做什么? -
您的代码格式不正确。你能按原样格式化吗? @MartijnPieters 我相信第一个
for下面的整个代码部分是错误嵌套的。 -
for tr in trs:之后的所有内容都应该在那个循环中吗?请相应缩进。
标签: python