gpt4 book ai didi

python - 为什么我会收到这个 NameError?我没有正确使用它吗?

转载 作者:行者123 更新时间:2023-12-04 16:19:39 28 4
gpt4 key购买 nike

我不确定我是否遗漏了什么或什么,但这一直给我这个错误。我已经阅读了一些问题,但它们似乎没有帮助。几天前我开始学习python,但我错过了什么?如果发生错误,我正在尝试排除错误以尝试其他方法。我曾在另一台计算机上运行此程序,但现在由于某种原因无法正常工作?我正在使用 anaconda 和 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]'

最佳答案

在您发布详细跟踪之前,这是我注意到的,而且只是乍一看。您的代码中可能缺少某些缩进。
- 您的“with open(file)”没有与之相关的事件。任何属于“with open”的函数调用和赋值都需要在它下面缩进。
- “for name,email in names”也有同样的问题
- “if (i > entries) : break”跳出一个不存在的循环。我猜“for name,email”下的所有内容都应该缩进,因为它属于它?

看起来 ControlNotFoundError 是在 Mechanize 中定义的
将其引用为“mechanize._form.ControlNotFoundError”

关于python - 为什么我会收到这个 NameError?我没有正确使用它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47026622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com