Search code examples
pythonpython-3.xpython-2.7beautifulsouphtml-parsing

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.


Solution

  • 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']