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 fulllinkの外にあるfor

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 に答える