Skip to content
TheChatArchiveThe Chat Archive
Metallama-3.3-70b-instruct· MetaHugging Face LMArena Human Preference 140K

How to find a tag by specific text in Python BeautifulSoup?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-instruct-2507’s answer.
  1. User

    How to find a tag by specific text in Python BeautifulSoup?

  2. Meta Assistant

    To find a tag by specific text in Python BeautifulSoup, you can use the following methods:

    1. Using the find() method with text attribute

    You can use the find() method and specify the text attribute to search for a tag containing specific text.

    from bs4 import BeautifulSoup
    
    html = '<p>Hello World!</p><p>This is another paragraph.</p>'
    soup = BeautifulSoup(html, 'html.parser')
    
    # Find the tag containing the text "Hello World!"
    tag = soup.find(lambda tag: tag and "Hello World!" in tag.text)
    
    print(tag)  # Output: <p>Hello World!</p>
    

    2. Using the find_all() method with text attribute

    If there are multiple tags containing the same text, you can use the find_all() method to find all of them.

    from bs4 import BeautifulSoup
    
    html = '<p>Hello World!</p><p>Hello World! Again.</p><p>This is another paragraph.</p>'
    soup = BeautifulSoup(html, 'html.parser')
    
    # Find all tags containing the text "Hello World!"
    tags = soup.find_all(lambda tag: tag and "Hello World!" in tag.text)
    
    for tag in tags:
        print(tag)
    # Output:
    # <p>Hello World!</p>
    # <p>Hello World! Again.</p>
    

    3. Using the find() method with string attribute

    You can also use the find() method with the string attribute to search for a tag containing specific text.

    from bs4 import BeautifulSoup
    
    html = '<p>Hello World!</p><p>This is another paragraph.</p>'
    soup = BeautifulSoup(html, 'html.parser')
    
    # Find the tag containing the text "Hello World!"
    tag = soup.find('p', string='Hello World!')
    
    print(tag)  # Output: <p>Hello World!</p>
    

    Note that the string attribute will only match the exact text, whereas the text attribute will match any text that contains the specified string.

    4. Using regular expressions

    If you need to search for a tag containing text that matches a regular expression pattern, you can use the re module.

    from bs4 import BeautifulSoup
    import re
    
    html = '<p>Hello World!</p><p>This is another paragraph.</p>'
    soup = BeautifulSoup(html, 'html.parser')
    
    # Find the tag containing text that matches the regular expression pattern
    tag = soup.find(lambda tag: tag and re.search(r'Hello.*World', tag.text))
    
    print(tag)  # Output: <p>Hello World!</p>
    

    These methods can be used to find tags by specific text in Python BeautifulSoup. The choice of method depends on the specific requirements of your project.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.