在 'For' 中调用函数循环抛出 NameError [英] Calling a function inside 'For' loop throws a NameError

查看:60
本文介绍了在 'For' 中调用函数循环抛出 NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 FOR 循环中调用函数,但出现错误:

I'm trying to call a function from a FOR loop but get the error:

 test()
NameError: name 'test' is not defined

代码如下:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from lxml import html
import requests
import xlwt

browser = webdriver.Firefox() # Get local session of firefox

# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")


for link in links:
    link.send_keys(Keys.CONTROL + Keys.RETURN)
    link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
    time.sleep(5)
    test()
    link.send_keys(Keys.CONTROL + 'w')



def test(self):#test function
    elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

    for elem in elems:
        print elem.text
    elem1 = browser.find_elements_by_css_selector("div.content.clearfix>div>fieldset>div>ul>li>a")

    for elems21 in elem1:
        print elems21.text 
    return 0

所以我想调用该函数,当调用该函数时,我希望将数据复制/粘贴到 Excel 中.

So I want to call the function and when the function is called I want the data to be copy/pasted into Excel.

有人可以帮我改进代码吗?

Can somebody help me to enhance the code ?

推荐答案

您必须先创建一个函数对象,然后才能使用它.在您的情况下,您调用了一个函数,但它尚不存在,因此未定义.正如凯文所说,定义函数然后尝试调用它.

You have to create a function object before you can use it. In your case you call a function but it is not yet existing hence not defined. As Kevin said, define the function and then try to call it.

UPD:我无法添加评论,所以我在这里更新.Mark Lutz 在他的Learning Python"一书中非常详细地描述了它,函数是如何工作的,def 做了什么以及当你调用一个函数时会发生什么.但我认为任何其他 Python 书籍都会这样做.

UPD: I cannot add comments so I update it here. Mark Lutz in his "Learning Python" book describes it in a great detail, how functions work, what def does and what happens when you call a function. But I assume any other Python book will do the same.

UPD:写评论不容易,所以更新答案.

UPD: It is not easy to write comments, so I update the answer.

如前所述,问题在于您在调用函数后定义了它.例如,假设我想编写一个程序,上面写着玩得开心"+ 任何名字.为简单起见,程序中给出了名称.选项 1:如果我像你一样编写程序意味着 1) 调用一个函数 2) 定义一个函数,我会得到一个与你完全一样的 NameError.

As said the problem is that you define function after you call it. For example, let's assume I want to write a program which writes "Have fun, " + any name. For simplicity name is given in the program. Option 1: If I write the program like you did meaning 1) call a function 2) define a function I will get an NameError exactly like you get.

程序:

greet = 'Have fun, '

print(greet + name('John')) # I call a function 'name'

def name(x):                # I define a function
    return str(x)

输出将是:

Traceback (most recent call last):
  File "C:/Users/nikolay.dudaev/Documents/Private/deffun2.py", line 3, in <module>
    print(greet + name('John'))
NameError: name 'name' is not defined

我需要做的就是更改函数定义的位置并调用函数:

ALL that I need to do is change places of function definition and calling a function:

greet = 'Have fun, '

def name(x):                # I define a function
    return str(x)

print(greet + name('John')) # I call a function 'name'

现在输出是:

======= RESTART: C:/Users/nikolay.dudaev/Documents/Private/deffun2.py =======
Have fun, John
>>> 

给你!

复制 def 之后的内容并将其粘贴到 for 循环之前,它应该可以工作(虽然我没有尝试你的代码).

Copy what you have after def and paste it before your for loop and it should work (though I did not try your code).

这篇关于在 &amp;#39;For&amp;#39; 中调用函数循环抛出 NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆