Split an element with BeautifulSoup

人走茶凉 提交于 2021-02-07 19:28:27

问题


I have some html code that I'm parsing with BeautifulSoup. One of the requirements is that tags are not nested in paragraphs or other text tags.

For example if I have a code like this:

<p>
    first text
    <a href="...">
        <img .../>
    </a>
    second text
</p>

I need to transform it into something like this:

<p>first text</p>
<img .../>
<p>second text</p>

I have done something to extract the images and add them after the paragraph, like this:

for match in soup.body.find_all(True, recursive=False):                
    try:            
        for desc in match.descendants:
            try:
                if desc.name in ['img']:      

                    if (hasattr(desc, 'src')):                            
                        # add image as an independent tag
                        tag = soup.new_tag("img")
                        tag['src'] = desc['src']

                        if (hasattr(desc, 'alt')):
                            tag['alt'] = desc['alt']
                        else
                            tag['alt'] = ''

                        match.insert_after(tag)

                    # remove image from its container                            
                    desc.extract()

            except AttributeError:
                temp = 1

    except AttributeError:
        temp = 1

I have written another piece of code that deletes empty elements (like the tag that is left empty after its image is removed), but I have no idea how to split the element into two different ones.


回答1:


import string
the_string.split(the_separator[,the_limit])

this will produce an array so you can either go trough it with for loop or get elements manualy.

  • the_limit is not required

In your case I think that the_separator need to be "\n" But that depends from case to case. Parsing is very interesting yet sometimes a dificult thing to do.




回答2:


from bs4 import BeautifulSoup as bs
from bs4 import NavigableString
import re

html = """
<div>
<p> <i>begin </i><b>foo1</b><i>bar1</i>SEPATATOR<b>foo2</b>some text<i>bar2 </i><b>end </b> </p>
</div>
"""
def insert_tags(parent,tag_list):
    for tag in tag_list:
        if isinstance(tag, NavigableString):
            insert_tag = s.new_string(tag.string)
            parent.append(insert_tag)
        else:
            insert_tag = s.new_tag(tag.name)
            insert_tag.string = tag.string
            parent.append(insert_tag)

s = bs(html)
p = s.find('p')
print s.div
m = re.match(r"^<p>(.*?)(SEPATATOR.*)</p>$", str(p))
part1 = m.group(1).strip()
part2 = m.group(2).strip()

part1_p = s.new_tag("p")
insert_tags(part1_p,bs(part1).contents)

part2_p = s.new_tag("p")
insert_tags(part2_p,bs(part2).contents)

s.div.p.replace_with(part2_p)
s.div.p.insert_before(part1_p)
print s.div

Works fine with me as I don't use nested HTML for that purpose. Admittedly, it still looks awkward. It produces in the my example

<div>
<p><i>begin </i><b>foo1</b><i>bar1</i></p>
<p>SEPATATOR<b>foo2</b>some text<i>bar2 </i><b>end </b></p>
</div>


来源:https://stackoverflow.com/questions/12616912/split-an-element-with-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!