dragonmnl
dragonmnl

Reputation: 15548

BeautifulSoup4 - python: how to merge two bs4.element.ResultSet and get one single list?

I have two

bs4.element.ResultSet

objects.

Let's call them

rs1
rs2

I want one result set (let's call it rs) with all the results in resultset.

I also need to figure out:

Upvotes: 1

Views: 5828

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122232

A bs4.element.ResultSet object is a straight-up subclass of list. You can use ResultSet.extend() to extend one or the other resultset:

rs1.extend(rs2)

or simply concatenate the two result sets:

newlist = rs1 + rs2 

The latter creates a list object with the contents of the two result sets, which means you'll lose the .source attribute. Not a great loss, really, seeing as nothing in BeautifulSoup itself uses that attribute.

There are ways to create just the one result set to begin with, rather than concatenate the two. Searches that can find either result type would lead to the results being returned in document source order, rather than back to back. You can use list arguments to the find_all() method, for example:

soup.find_all(['a', 'link'], href=True)

would find all a and link elements with a href attribute, for example.

Upvotes: 11

Related Questions