Last modified: Oct 29, 2023 By Alexander Williams

BeautifulSoup: Working with <li> Tags

Example 1: Find All <li> Tags


from bs4 import BeautifulSoup

html = """
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
"""

# Create a BeautifulSoup object to parse the HTML
soup = BeautifulSoup(html, 'html.parser')

# Find all <li> tags within the <ul> element
list_items = soup.find_all('ul')[0].find_all('li')

# Print the text inside each <li> tag
for li in list_items:
    print(li.get_text())

Output:


Item 1
Item 2
Item 3
    

Example 2: Find <li> Tags by Class


html = """
<ul>
    <li class="important">Important Item</li>
    <li>Normal Item 1</li>
    <li>Normal Item 2</li>
</ul>
"""

soup = BeautifulSoup(html, 'html.parser')

# Find <li> tags with the "important" class
important_items = soup.find_all('ul')[0].find_all('li', class_='important')

# Print the text inside the matching <li> tag
for li in important_items:
    print(li.get_text())

Output:


Important Item
    

Example 3: Find Nested <li> Tags


html = """
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <ul>
        <li>Sub-item 1</li>
        <li>Sub-item 2</li>
    </ul>
</ul>
"""

soup = BeautifulSoup(html, 'html.parser')

# Find the parent <ul> element
parent_list = soup.find_all('ul')[0]

# Find all <li> tags within the parent <ul> element
list_items = parent_list.find_all('li')

# Print the text inside each <li> tag
for li in list_items:
    print(li.get_text())

Output:


Item 1
Item 2
Item 3
Sub-item 1
Sub-item 2
    

Example 4: Find <li> Tags by Index


html = """
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ul>
"""

soup = BeautifulSoup(html, 'html.parser')

# Find <li> tags by index
items = soup.find_all('ul')[0].find_all('li')
third_item = items[2]

# Print the text inside the third <li> tag
print(third_item.get_text())

Output:


Item 3
    

Example 5: Find First <li> Tag with Specific Text


html = """
<ul>
    <li>Apple</li>
    <li>Banana</li>
    <li>Cherry</li>
    <li>Apple</li>
</ul>
"""

soup = BeautifulSoup(html, 'html.parser')

# Find the first <li> tag with specific text
target_text = "Apple"
first_apple = soup.find('ul').find('li', text=target_text)

# Print the text inside the first matching <li> tag
print(first_apple.get_text())

Output:


Apple
    

Example 6: Find All <li> Tags and Their Attributes


html = """
<ul>
    <li class="item" id="item-1">Item 1</li>
    <li class="item" id="item-2">Item 2</li>
    <li class="item" id="item-3">Item 3</li>
</ul>
"""

soup = BeautifulSoup(html, 'html.parser')

# Find all <li> tags and print their attributes
list_items = soup.find('ul').find_all('li')
for li in list_items:
    print(f"Text: {li.get_text()}")
    print(f"Class: {li['class'][0]}")
    print(f"ID: {li['id']}\n")

Output:


Text: Item 1
Class: item
ID: item-1

Text: Item 2
Class: item
ID: item-2

Text: Item 3
Class: item
ID: item-3