2017-10-31 2 views
1

무언가 또는 무엇을 놓치고 있는지 확실하지 않지만이 오류가 계속 발생합니다. 몇 가지 질문을 읽었지만 도움이되지 않는 것 같습니다. 나는 며칠 전에 파이썬을 배우기 시작했는데, 무엇이 빠졌는가? 오류가 발생하면 다른 것을 시도하기 위해 오류를 제외하려고합니다. 나는 다른 컴퓨터에서이 작업을했지만 지금은 어떤 이유로 작동하지 않습니까? 아나콘다와 쥬피터를 사용하고 있습니다.왜이 NameError가 발생합니까? 올바르게 사용하지 않습니까?

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]' 
+0

로 참조하면에서 해당 예외를 가져와야처럼 오류 코드, – 0TTT0

+0

는 0TTT0 –

+0

@ 내 역 추적을 추가 전체 역 추적 보이는 게시하시기 바랍니다 정의 된 곳. –

답변

-1

내가 눈치 무엇이며, 그것은 단지 첫눈에 있습니다. 코드에 들여 쓰기가 누락되었을 수 있습니다.
- "with open (file)"에 연결된 활동이 없습니다. "open with"내에있는 모든 함수 호출 및 할당은 그 아래에 들여 쓰기해야합니다.
- "이름, 전자 메일 이름"에 동일한 문제가 있습니다. - "if (i> entries) : break"가 존재하지 않는 루프에서 벗어납니다. "이름, 이메일"아래에있는 모든 내용이 속한 것처럼 들여 쓰기해야합니다. ControlNotFoundError가 기계화에 정의 된 것처럼

이 보이는 "mechanize._form.ControlNotFoundError"

+0

전 전체 추적을 게시 했습니까? 내 코드 하단에 있습니다 –

+0

원래 질문에 답할 수없는 보조 메모가있는 경우 답변이 아닌 의견으로 게시하십시오. 질문과 관련이없는 답변은 투표에 실패 할 수 있습니다. –

+0

포인터 Klaus D. 주셔서 감사합니다 – Joel

관련 문제