is there any way to transform bs4.element.resultset TO a single bs4.element.Tag

Viewed 16

I have to scrap a page with this structure:

<HTML>
...
<div id="sub">
...useless stuff
     <div class="container">useless stuff</div>
...useless stuff
     <div class="container">useless stuff</div>
...useless stuff
     <div class="container">useless stuff</div>
</div>
<div id="main">
  ...useless stuff
     <div class="container">useful info</div>
  ...useless stuff
     <div class="container">useful info</div>
  ...useless stuff
     <div class="container">useful info</div>
  ...useless stuff
     <div class="container">useful info</div>
</div>
...
</html>

I need the content inside container, but only the ones inside main for that I have this piece of code:

content = root.find('div', {"id": "main"}).find_all('div', {"class": "container"})

which is working, I'm receiving a list of bs4.element.Tag with the info that I need, however, my method needs to return a bs4.element.Tag and not a bs4.element.resultset, otherwise it's gonna break a lot of stuff on other methods beyond my control

So, what I want to do is to concatenate all the bs4.element.Tag inside the resultSet on a single bs4.element.Tag, just adding a <h2> between them, for example

I have this:

resultSet = [<div class="container">CONTENT1</div>, 
             <div class="container">CONTENT3</div>, 
             <div class="container">CONTENT3</div>]

I want this:

content = <div class="container">CONTENT1</div> <h2> 
<div class="container">CONTENT3</div> <h2> 
<div class="container">CONTENT3</div>

type(content) = <class 'bs4.element.Tag'>

I have tried to use a simple for loop, but I end with a STR, and I need to be a bs4.element.Tag type

1 Answers

You can construct new soup from the filtered tags, for example:

content = BeautifulSoup(
    "\n".join(map(str, soup.select("#main .container"))), "html.parser"
)

print(content)
print(type(content))

Prints:

<div class="container">useful info</div>
<div class="container">useful info</div>
<div class="container">useful info</div>
<div class="container">useful info</div>
<class 'bs4.BeautifulSoup'>
Related