2

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

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 行的循环中不正确。我正在使用 notepad++ 和 windows powershell。如何使此代码工作?

4

5 回答 5

2

自下而上的一切都print fulllinkfor循环之外

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
于 2013-10-21T03:51:07.860 回答
1

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

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
于 2013-10-21T03:51:10.330 回答
1

空格在 python 中很重要。

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

for tr in trs:
    for link in tr.find_all('a'):
        fulllink = link.get ('href')
print fulllink #print in terminal to verify results

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

for tr in trs:
    for link in tr.find_all('a'):
        fulllink = link.get ('href')
        print fulllink #print in terminal to verify results
于 2013-10-21T03:51:28.847 回答
0

您必须在 for 循环的缩进之外将代码缩进另一个缩进级别(即 4 个空格/1 个制表符)。try/except 不是我的 for 循环,这就是你得到 continue 错误的原因。

缩进显示块在一起的位置(for循环开始一个新块,你需要在它下面缩进)

于 2013-10-21T03:53:16.030 回答
0

我的回答可能很简单,但它确实不在循环上,它必须在循环上,就像 break 在条件和循环上的工作方式一样。也许你的缩进已经关闭,这是一个很大的必须并且在 python 中非常重要。

于 2013-10-21T03:53:19.703 回答