我和BeautifulSoup一起工作,我一直在循环中得到一个错误continue not properly。所以我删除了continue,然后得到了print语句的无效语法错误。我运行的是BS4和Python2.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
打印/继续时缩进将关闭。如果它是关闭的,except:将看起来像是空的,我不确定Python是否对此满意。在
试着注释掉与Try/except无关的所有内容,看看它是否仍然给出错误。在
既然你似乎有错误,我相信你可能真的在你的文件中有错误的缩进。您的代码应该如下所示:
在python中,每个代码块都应该使用制表符/空格进行缩进嵌套。把它们混在一起不好。在
在代码中,第一个for循环将遍历所有tr,第二个循环打印所有url。在
但是你忘了缩进第一个应该在for循环中的块。在
编辑
你也不必在你的案例中使用continue。检查我对你代码的编辑。在
相关问题 更多 >
编程相关推荐