0

我正在使用以下代码:

from bs4 import BeautifulSoup
import csv
soup = BeautifulSoup (open("43rd-congress.htm"))

final_link = soup.p.a
final_link.decompose()


f = csv.writer(open("43rd_congress_all.csv", "w"))
f.writerow(["Name","Years","Position","Party", "State", "Congress", "Link"])
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
        f.writerow([names, years, posiitons, parties, states, congress, fullLink])

我收到以下错误。在第 34 行(这是最后一行)。IndentationError:意外缩进。我在这里使用本教程。http://jeriwieringa.com/blog/2012/11/04/beautiful-soup-tutorial-part-1/

我怀疑这是缩进的问题。使用以下代码将在第 9 行出现错误,其中 csv 未定义。

4

1 回答 1

4

错误非常明显。

    print names, years, positions, parties, states, congress
        f.writerow([names, years, posiitons, parties, states, congress, fullLink])

应该

    print names, years, positions, parties, states, congress
    f.writerow([names, years, posiitons, parties, states, congress, fullLink])

Python 不喜欢代码的意外缩进,并且应该与语句f.writerow具有相同的缩进级别。print

在此处阅读有关缩进的更多信息。我建议您在完全理解这一点之前不要再继续,因为它是 python 中最基本和最重要的概念之一。

要修复 csv 问题,

你需要

import csv

在文件的开头。

于 2013-10-21T04:32:56.123 回答