0
所以这是代码即时提供和说明如下:)Python - For循环所有,然后打印?
r = s.get(Url)
names = soup(r.text, 'html.parser')
findName = names.find('div', {'class':'nameslist'})
if (r.status_code == 200):
for name in names.find_all('option'):
global nameID
if myName == foundName:
nameID = size.get('value')
print('Profile-1 Found name')
break
else:
print('Did not find name')
if (findName != None):
notepresent = True
print('There is a value')
else:
global addtonotes
notepresent = False
addtonotes = {'id': nameID}
add2notes(addtonotes)
输出
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Profile-1 Found name
我要的是,它不应该说“找不到名称”直到它通过循环遍历所有名字,然后说它没有找到任何名字。 但是它现在所做的是,它为每个名称逐个循环,直到找到名称,然后我使用break来不再搜索其他名称,因为它不需要。
所以问题是我怎么能把它打开,所以它会说没有找到名称时,它已经为每个名称循环,然后说没有找到名称后,它已通过所有名称循环?
编辑:
所以每当它找到的名称就会去addtonotes = {'id': nameID}
,然后它会被添加到addtonotes但它不发现则会有一个回溯说:
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Traceback (most recent call last):
self._target(*self._args, **self._kwargs)
File "C:\Users\Test.py", line 153
addtonotes = {'id': nameID}
NameError: name 'nameID' is not defined
所以我也想要的是,当它没有找到任何名称时,它应该只使用Sys.exit(),但我现在不能在else语句中实现它,因为它会通过第一个循环执行sys.exit ...
我希望有人认识这个问题:)
哦。它有助于提升,但是现在发生的事情是,它说它找到了名字,但是也表示它没有被发现。输出将会显示'''Profile-1 Found name - 没有找到名字'''但是这是你做的第二次编辑。 – WeInThis
@WeInThis:我发布的代码无法同时打印;要么打印“Profile-1 Found name”,要么执行“break”,要么到达'else'块。它不能做到这一点,因为“break”。 –
哦,哦。好吧,我确实尝试了第一种解决方案,似乎即使没有休息也能得到我想要的结果:)我认为我可以将此标记为已解决。我还不确定,但我会测试周围,看看。到现在为止还挺好! :) – WeInThis