Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到这个名字错误?我用得不对吗?_Python_Python 2.7_Mechanize_Nameerror - Fatal编程技术网

Python 为什么我会得到这个名字错误?我用得不对吗?

Python 为什么我会得到这个名字错误?我用得不对吗?,python,python-2.7,mechanize,nameerror,Python,Python 2.7,Mechanize,Nameerror,我不确定我是否遗漏了什么,但这一直给我这个错误。我读了一些问题,但它们似乎没有帮助。几天前我开始学习python,但我还缺少什么?如果发生错误,我将尝试除错误之外的其他操作。我在另一台电脑上工作,但现在不知什么原因它不工作了?我用的是蟒蛇和jupyter import mechanize from mechanize import ControlNotFoundError import sys from random import randint import time import csv

我不确定我是否遗漏了什么,但这一直给我这个错误。我读了一些问题,但它们似乎没有帮助。几天前我开始学习python,但我还缺少什么?如果发生错误,我将尝试除错误之外的其他操作。我在另一台电脑上工作,但现在不知什么原因它不工作了?我用的是蟒蛇和jupyter

import mechanize
from mechanize import ControlNotFoundError
import sys
from random import randint
import time
import csv

ipa=randint(1, 254)
ipb=randint(1, 254)

ip="131.156." + str(ipa) + "." + str(ipb)

ofile = "D:\\Downloads\\filename.csv"

url = "https://website.com"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots

names=[]
i=0
entries=80
with open(ofile) as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
    names.append((row[0],row[1]))
csvDataFile.close()

for name,email in names:

ipa=randint(1, 254)
ipb=randint(1, 254)
ip="131.156." + str(ipa) + "." + str(ipb)    

br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url) 

br.select_form(nr = 0)
while True:
    try:
        br.form.find_control( 'Entries[registration_ip]').readonly = False
        print ("Form found ")
        break
    except ControlNotFoundError:
        print ("ControlNotFoundError")
        br.open(url)
        br.select_form(nr = 0)
        br.form.find_control( 'Entries[registration_ip]').readonly = False
br.form.set_value(ip, 'Entries[registration_ip]')
br.form.set_value(name, 'Entries[full_name]') 
br.form.set_value(email, 'Entries[email]')

time.sleep(randint(1, 110))
time.sleep(random())

res = br.submit()
print (name + " " + email) 

content = res.read()
if (i > entries) : break
i+=1

time.sleep(randint(1, 200))
time.sleep(random())

with open("mechanize_results.html", "w") as f:
f.write(content)
错误:

ControlNotFoundError                      Traceback (most recent call last)
<ipython-input-18-e095be0c9792> in <module>()
 47             br.open(url)
 48             br.select_form(nr = 0)
---> 49             br.form.find_control( 
'Entries[registration_ip]').readonly = 
False
 50     br.form.set_value(ip, 'Entries[registration_ip]')
 51     br.form.set_value(name, 'Entries[full_name]')

C:\Users\Donald\Anaconda2\lib\site-packages\mechanize\_form_controls.pyc in 
find_control(self, name, type, kind, id, predicate, nr, label)
2329             raise ValueError(
2330                 "at least one argument must be supplied to specify 
control")
-> 2331         return self._find_control(name, type, kind, id, label, 
predicate, nr)
2332 
2333 # ---------------------------------------------------

C:\Users\Donald\Anaconda2\lib\site-packages\mechanize\_form_controls.pyc in 
_find_control(self, name, type, kind, id, label, predicate, nr)
2422                                  description)
2423         elif not found:
-> 2424             raise ControlNotFoundError("no control matching " + 
description)
2425         assert False
2426 
ControlNotFoundError: no control matching name 'Entries[registration_ip]'
ControlNotFoundError回溯(最近一次调用)
在()
47 br.打开(url)
48 br.选择表格(nr=0)
--->49 br.表格查找控件(
“条目[registration_ip]”。只读=
错误的
50 br.表单设置值(ip,'条目[注册ip]')
51 br.form.set_值(名称,“条目[全名]”)
C:\Users\Donald\Anaconda2\lib\site packages\mechanize\\u form\u controls.pyc in
查找控件(self、name、type、kind、id、谓词、nr、label)
2329上升值错误(
2330“必须至少提供一个参数来指定
控制“)
->2331返回自我。查找控制(名称、类型、种类、id、标签、,
谓词,nr)
2332
2333 # ---------------------------------------------------
C:\Users\Donald\Anaconda2\lib\site packages\mechanize\\u form\u controls.pyc in
_find_控件(self、name、type、kind、id、label、predicate、nr)
2422(说明)
2423未找到elif:
->2424 raise ControlNotFoundError(“无控制匹配”+
(说明)
2425断言错误
2426
ControlNotFoundError:没有与名称“条目[registration\u ip]”匹配的控件

在您发布详细跟踪之前,以下是我注意到的内容,这只是第一眼看到的。代码中可能缺少一些缩进。
-您的“打开(文件)”没有与之关联的活动。任何属于“with open”的函数调用和分配都需要缩进到它下面。
-“对于名称,名称中的电子邮件”也有同样的问题 -“if(i>entries):break”中断不存在的循环。我猜“姓名,电子邮件”下的所有内容都应该缩进,因为它属于它

看起来ControlNotFoundError是在mechanize中定义的
将其引用为“mechanize.\u form.ControlNotFoundError”

请发布错误代码,整个回溯添加了my traceback@0TTT0看起来您需要从定义该异常的位置导入该异常。您能详细说明吗?我不知道你是什么意思@KlausD。你修复了一个错误,代码中有几行代码,出现了一个新的无关错误。一个新的错误意味着你应该打开一个新的问题。不幸的是,你已经删除了原始的错误消息,这使得这个问题对有相同问题的人不太有用。我确实发布了我的完整跟踪,不是吗?这是在我的代码底部如果你有旁注,将不会回答原来的问题,请发表评论,而不是作为一个答案。与问题无关的答案可能会被否决。我编辑了我的答案,实际上包含了一个“答案”。请说明否决投票的原因。