Python 缩进误差?或名称未定义

Python 缩进误差?或名称未定义,python,Python,当我使用以下代码时 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", "Sta

当我使用以下代码时

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])
我得到一个名字错误。然而,当我试图纠正错误时,我在代码的最后一行得到一个错误,表示变量未定义。我已经做了更正,以使它与社区保持一致。我怎么修理它

我感谢你的帮助

我在记事本++和powershell中运行这个。我在本教程的最后一节中…

如果try/except子句的第一行出现错误,则将永远不会创建名称、年份、职位、政党、州和国会

发生的情况是在try结构期间引发了一个错误。假设names=strtds[0]。get\u text会创建一个错误。您捕获了它,但后一个变量永远不会被创建

您可能想考虑在尝试/默认之前生成默认值,例如No.=,

您的缩进错误可能只是因为制表符和空格的混合,因为您的代码在我看来很好

    #                       |-> Different from when passed below
    print names, years, positions, parties, states, congress
    f.writerow([names, years, posiitons, parties, states, congress, fullLink])
    #                             |-> Different from original name    |-> Same with fullLink, its supposed to be called fullink when instantiated.
在上面的例子中,位置和位置是不同的。这是一个简单的打字错误

看看下面的代码,看看它是否运行,因为我没有你的文件

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.
            # This structure isolate the item by its column in the table and converts it into a string
            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()

            print names, years, positions, parties, states, congress
            f.writerow([names, years, positions, parties, states, congress, fullLink])
        except IndexError:
            print "bad tr string"
            continue  # This tells the computer to move on to the next item after it encounters an error

将来,只需复制并粘贴完整的堆栈跟踪,而不是试图用自己的话描述错误。在你的例子中,最后一行似乎有两个拼写错误:posiitons和fullLink。谢谢你……在某个时刻,你永远不应该在午夜后编写代码。我真不敢相信我错过了这一切。