首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python BeautifulSoup html5lib mix似乎删除了for循环中的所有其他项

Python BeautifulSoup html5lib mix似乎删除了for循环中的所有其他项
EN

Stack Overflow用户
提问于 2016-06-19 06:08:13
回答 1查看 29关注 0票数 0

我是python的新手,但到目前为止我真的很喜欢这种语言。

我已经创建了一系列复杂的html5元素并使用了html5lib模块。

当我浏览段落中的元素时,我可以很好地将它们打印出来,但是当我尝试使用BS4的insert方法时,我得到的只是其他元素的输出,我不知道为什么!

我的python:

代码语言:javascript
复制
i = 0
    for gallery_elem in gallery_header_next_sibling:
        if ( gallery_elem.name.lower() == 'img' ):
            if ( i == 0 ):
                new_gallery = soup.new_tag( "div" )
                new_gallery[ "class" ] = "gallery"

            new_gallery_elem = soup.new_tag( "figure" )

            if ( gallery_elem.has_attr( "alt" ) ):
                new_gallery_cap = soup.new_tag( "figcaption" )
                new_gallery_cap.string = gallery_elem[ "alt" ]
                new_gallery_elem.insert( 2, new_gallery_cap )

            if ( gallery_elem.has_attr( "title" ) ):
                new_gallery_attribution = soup.new_tag( "dl" )
                new_gallery_attribution_dt = soup.new_tag( "dt" )
                new_gallery_attribution_dt.string = "Image owner:"
                new_gallery_attribution_dd = soup.new_tag( "dd" )
                new_gallery_attribution_dd.string = gallery_elem[ "title" ]
                new_gallery_attribution.insert( 0, new_gallery_attribution_dt )
                new_gallery_attribution.insert( 1, new_gallery_attribution_dd )

        new_gallery_elem.insert( 1, new_gallery_attribution )
        new_gallery_elem.insert( 1, gallery_elem )
        i = i + 1

    new_gallery_elem.insert( 1, gallery_elem )

HTML

代码语言:javascript
复制
<img alt="Caption One." src="img/orange.jpg" title="Attribution One."/>
<img alt="Caption Two." src="img/red.jpg" title="Attribution Two."/>
<img alt="Caption Three." src="img/urban.jpg" title="Attribution Three."/>
<img alt="Caption Four." src="img/brolly.jpg" title="Attribution Four."/>
<img alt="Caption Five." src="img/tomy.jpg" title="Attribution Five."/>

输出:

代码语言:javascript
复制
<figure><figcaption>Caption One.</figcaption><img alt="Caption One." src="img/orange.jpg" title="Attribution One."/><dl><dt>Image owner:</dt><dd>Attribution One.</dd></dl></figure>
<figure><figcaption>Caption Three.</figcaption><img alt="Caption Three." src="img/urban.jpg" title="Attribution Three."/><dl><dt>Image owner:</dt><dd>Attribution Three.</dd></dl></figure>
<figure><figcaption>Caption Five.</figcaption><img alt="Caption Five." src="img/tomy.jpg" title="Attribution Five."/><dl><dt>Image owner:</dt><dd>Attribution Five.</dd></dl></figure>

如果我拉出下面这一行,我得到了所有五个元素。有没有人知道我做错了什么?

代码语言:javascript
复制
new_gallery_elem.insert( 1, gallery_elem )
EN

回答 1

Stack Overflow用户

发布于 2016-06-19 07:57:55

因此,经过一些实验后,我发现,如果我将所需的元素存储在列表中,然后从列表中检索它们,而不是尝试实时编辑汤汁,这就解决了我的问题。

一旦创建并存储了对象,就可以将它们重新添加到先前创建的父元素中,并将其插入到soup中。

我希望这为别人解决了一些过早的脱发…

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37902071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档