首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python继续循环

Python继续循环
EN

Stack Overflow用户
提问于 2013-10-21 11:47:50
回答 5查看 313关注 0票数 2

我使用本教程中的以下代码(http://jeriwieringa.com/blog/2012/11/04/beautiful-soup-tutorial-part-1/)。

代码语言:javascript
复制
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. This allows the program to continue after encountering an error.
    names = str(tds[0].get_text()) # This structure isolate the item by its column in the table and converts it into a string.
    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 #This tells the computer to move on to the next item after it encounters an error

print names, years, positions, parties, states, congress

但是,我在第27行收到一个错误,指出'continue‘没有正确地出现在循环中。我使用的是notepad++和windows powershell。我如何让这段代码工作呢?

EN

回答 5

Stack Overflow用户

发布于 2013-10-21 11:51:08

print fulllink开始的所有东西都在for循环之外

代码语言:javascript
复制
for tr in trs:
    for link in tr.find_all('a'):
        fulllink = link.get ('href')
    ## indented here!!!!!
    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. This allows the program to continue after encountering an error.
        names = str(tds[0].get_text()) # This structure isolate the item by its column in the table and converts it into a string.
        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 #This tells the computer to move on to the next item after it encounters an error

    print names, years, positions, parties, states, congress
票数 2
EN

Stack Overflow用户

发布于 2013-10-21 11:51:10

看起来你的缩进关闭了,试试这个。

代码语言:javascript
复制
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. This allows the program to continue after encountering an error.
            names = str(tds[0].get_text()) # This structure isolate the item by its column in the table and converts it into a string.
            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 #This tells the computer to move on to the next item after it encounters an error

        print names, years, positions, parties, states, congress
票数 1
EN

Stack Overflow用户

发布于 2013-10-21 11:51:29

空格在python中很重要。

这就是事情走下坡路的地方:

代码语言:javascript
复制
for tr in trs:
    for link in tr.find_all('a'):
        fulllink = link.get ('href')
print fulllink #print in terminal to verify results

您应该开始并继续使用适当数量的制表符来缩进代码,只要您打算循环。

代码语言:javascript
复制
for tr in trs:
    for link in tr.find_all('a'):
        fulllink = link.get ('href')
        print fulllink #print in terminal to verify results
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19485961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档