Python函数名称...未定义

时间:2019-03-02 15:53:34

标签: python python-3.x function

我正在执行一些python Web内容请求,我想在代码中进行一些功能,但是有一个错误,我不知道为什么显示。 我的代码如下:

def tempRequest(tree, heading):

    page = requests.get("http://10.0.0.3/admin/speedtest.php")
    tree = html.fromstring(page.content)
    heading = tree.xpath('//a[@id="temperature"]/text()')
    return heading, tree

    tempRequest(tree, heading)
    heading = tree.xpath('//a[@id="temperature"]/text()')

    sheet = client.open("Database").sheet1

    sheet.insert_row(heading, 10)
    time.sleep(5)
  

tempRequest(树,标题)NameError:未定义名称“树”

你们能帮我吗?谢谢。

1 个答案:

答案 0 :(得分:3)

您的代码中有一些基本错误,应该是这样的:

def tempRequest():
    page = requests.get("http://10.0.0.3/admin/speedtest.php")
    tree = html.fromstring(page.content)
    heading = tree.xpath('//a[@id="temperature"]/text()')
    return heading, tree

heading, tree = tempRequest()
sheet = client.open("Database").sheet1

sheet.insert_row(heading, 10)
time.sleep(5)

在原始代码中,您试图在代码中定义变量之前将变量传递给函数。而且您根本没有使用函数返回值。