【问题标题】:Why is ControlNotFoundError occuring in my code?为什么我的代码中出现 ControlNotFoundError?
【发布时间】:2017-10-31 03:13:42
【问题描述】:

现在我很困惑,我通过添加“从机械化导入 ControlNotFoundError.但是,它现在弹出此错误。任何帮助是极大的赞赏。几天前我刚开始学习python,所以我对这些错误不是很熟悉,而且我认为我还没有在这里找到类似的东西。

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\csvDataFile.csv"

url = "https://website.com/realsite"
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 mechanize.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-6-549eea94ea1f> in <module>()
 46             br.open(url)
 47             br.select_form(nr = 0)
---> 48             br.form.find_control( 
'Entries[registration_ip]').readonly = False
 49     br.form.set_value(ip, 'Entries[registration_ip]')
 50     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]'

【问题讨论】:

  • 请修正你的缩进。
  • 固定缩进@gommb

标签: python jupyter-notebook mechanize


【解决方案1】:
try:
    br.form.find_control( 'Entries[registration_ip]').readonly = False
    print "Form found ",
    break
except mechanize.ControlNotFoundError:
    print "ControlNotFoundError"
    br.open(url)
    br.select_form(nr = 0)
    br.form.find_control( 'Entries[registration_ip]').readonly = False

如果br.form.find_control() 在第一次被调用时抛出了ControlNotFoundError 异常,为什么当你在except 块内再次调用它时它会神奇地工作?

【讨论】:

  • 不太清楚,前几天刚开始学python。那你有什么建议呢?
  • 我假设您想跳过当前循环的其余部分并继续下一个循环迭代。很难提出更具体的建议,因为您的程序逻辑很难遵循——例如,url 总是相同的,那么为什么要在循环中调用br.open(url)?这样做的目的是什么?为什么不只打开一次?
  • 我想如果我输入多个带有姓名/电子邮件的数据条目,我不希望每个条目都重新打开它吗?
  • 好吧,也许你会的!我没有考虑到这一点。那就继续吧。
【解决方案2】:

奇怪。我决定尝试在我的 Mac 上运行它。我稍微改写了代码,让它变成这样。由于某种原因,它现在运行良好。

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

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

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

ofile="//Users//donaldkrambeck//Downloads//csvDataFiles.csv"


url = "http://website.com/realsite"
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, 100))
    time.sleep(random.random())

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-05
    • 2014-05-08
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多