如何修复“名称错误:名称‘checker_start’未定义”

2024-09-29 01:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

如何修复这些错误代码

Traceback (most recent call last): File "/Users/erzajullian/PycharmProjects/Checker/topmail.py", line 9, in class checker_start(object): File "/Users/erzajullian/PycharmProjects/Checker/topmail.py", line 16, in checker_start print(checker_start().get_token()) NameError: name 'checker_start' is not defined

这是密码

import requests
from bs4 import BeautifulSoup


class output(object):
    pass


class checker_start(object):
    def get_token(self):
        data = requests.get("https://mail.topmail.com/preview/mail/")
        soup = BeautifulSoup(data.text, "lxml")
        token_1 = soup.find("input", {"name": "form_token"})["value"]
        return token_1

    print(checker_start().get_token())

我的代码怎么了?在


Tags: inpytokengetobjectlinecheckerstart
2条回答

您的print(checker_start().get_token())的行缩进错误。您正在尝试实例化类checker_start的对象,并在类定义本身的代码块(作用域)中调用其方法get_token。因此你得到一个NameError。在

Python中最显著的特性之一是带有缩进的代码块。在Python中,缩进代码不是样式问题(就像在大多数编程语言中一样),而是一种需求。在

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to.

尝试:

import requests
from bs4 import BeautifulSoup


class output(object):
    pass


class checker_start(object):
    def get_token(self):
        data = requests.get("https://mail.topmail.com/preview/mail/")
        soup = BeautifulSoup(data.text, "lxml")
        token_1 = soup.find("input", {"name": "form_token"})["value"]
        return token_1

# remove the line-indentation
print(checker_start().get_token())

最后一行,print(checker_start().get_token()),缩进了一个级别,可能不应该这样。在

相关问题 更多 >