简体   繁体   English

Python执行一个“ for”循环,但不执行第二个

[英]Python executes one 'for' loop but not the second

My python code executes the first 'for' loop, but not the second. 我的python代码执行第一个“ for”循环,但不执行第二个。 If I comment out the first for loop, the second executes properly. 如果我注释掉第一个for循环,则第二个将正确执行。

Code: 码:

import urllib.request
from bs4 import BeautifulSoup 
from bs4 import NavigableString

site = urllib.request.urlopen('http://www.reddit.com/')
html = site.read()

soup = BeautifulSoup(html)
tags = soup.body.children

for item in tags:        
    print (item.name)
    print (item.attrs)

for item in tags:
    if ('role' in item.attrs and item.attrs['role'] == 'banner'):
        print (item)
    else: pass

any ideas? 有任何想法吗? My IDLE gui is acting strange tonight (shift+indent is indenting instead of undenting for example) so if this works for everyone else properly it might just be my box. 我的IDLE gui今晚的行为很奇怪(例如,shift + indent是缩进而不是缩进),因此,如果这对其他所有人都有效,那可能只是我的选择。

Thanks community! 谢谢社区!

I've never used BeautifulSoup, but it sounds a lot like tags is an iterator, not a list or something similar. 我从未使用过BeautifulSoup,但是听起来很像tags是一个迭代器,而不是列表或类似的东西。 That means, iter(tags) is tags and it has state that is altered by next() . 这意味着, iter(tags) is tags并且其状态由next()更改。 As a consequence, iterating over it consumes it irreparably. 结果,对其进行迭代将不可避免地消耗掉它。 I'd also wager that body.children is a property that creates a new iterator on each access, so you can run both loops if you turn it into a list first, or repeat tags = soup.body.children after the first loop. 我还要下注body.children是在每次访问中创建一个新迭代器的属性,因此,如果您首先将其转换为列表,则可以运行两个循环,也可以在第一个循环后重复执行tags = soup.body.children

tags is an iterable, but not a sequence; tags是可迭代的,但不是序列; the first for loop exhausts the iterable, leaving no elements for the second. 第一个for循环耗尽可迭代对象,第二个不保留任何元素。 Pass it to the list or tuple constructor to create a sequence from it, then iterate over that. 将其传递给listtuple构造函数以从中创建序列,然后对其进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM