Aklank Jain
Aklank Jain

Reputation: 1062

Beautiful Soup : How to get data which matches any of the given string

I am trying to find out element, which can match any of the input string.

For eg :-

data = soup.find(text="something")

This works perfectly fine, but how to use it when I have to search something like this:-

data = soup.find(text="something" or text="another something")

If not possible to search multiple strings, then what should be the best way to execute a similar thing.

Upvotes: 0

Views: 541

Answers (2)

facelessuser
facelessuser

Reputation: 1734

Regex is certainly a valid and useful way to search for multiple text, but people often forget (or don't know) that you can pass in a list of strings and Beautiful Soup will return results that matches any of the items in the list:

from bs4 import BeautifulSoup

html = """
<div>something</div>
<div>something else</div>
"""
soup = BeautifulSoup(html, "lxml")
items = soup.find_all(text=["something", "something else"])
print(items)

Output

['something', 'something else']

Upvotes: 3

Rakesh
Rakesh

Reputation: 82755

You can use Regex here.

Ex:

import re
data = soup.find(text=re.compile("something|another something"))

Upvotes: 2

Related Questions