Last modified: Oct 29, 2023 By Alexander Williams
BeautifulSoup extend() Method Examples
Example 1: Extending a List with Tags
from bs4 import BeautifulSoup
# HTML content with two div tags
html = "<div>First div</div><div>Second div</div>"
soup = BeautifulSoup(html, 'html.parser')
# Create an empty list
tag_list = []
# Find all div tags and extend the list with them
tag_list.extend(soup.find_all('div'))
# Print the list of tags
for tag in tag_list:
print(tag)
Output:
<div>First div</div>
<div>Second div</div>
Example 2: Combining Two Tag Lists
from bs4 import BeautifulSoup
# HTML content with two sets of div tags
html1 = "<div>First div</div><div>Second div</div>"
html2 = "<div>Third div</div><div>Fourth div</div>"
soup1 = BeautifulSoup(html1, 'html.parser')
soup2 = BeautifulSoup(html2, 'html.parser')
# Create two lists of tags
tag_list1 = soup1.find_all('div')
tag_list2 = soup2.find_all('div')
# Extend the first list with the tags from the second list
tag_list1.extend(tag_list2)
# Print the combined list of tags
for tag in tag_list1:
print(tag)
Output:
<div>First div</div>
<div>Second div</div>
<div>Third div</div>
<div>Fourth div</div>
Example 3: Extending a ResultSet with Find All
from bs4 import BeautifulSoup
# HTML content with a list of div tags
html = "<div>First div</div><div>Second div</div>"
soup = BeautifulSoup(html, 'html.parser')
# Find all div tags and extend the ResultSet with them
result_set = soup.find_all('div')
# Print the ResultSet
for tag in result_set:
print(tag)
Output:
<div>First div</div>
<div>Second div</div>
Example 4: Extending a ResultSet with Find
from bs4 import BeautifulSoup
# HTML content with a list of div tags
html = "<div>First div</div><div>Second div</div>"
soup = BeautifulSoup(html, 'html.parser')
# Find the first div tag and extend the ResultSet with it
result_set = soup.find('div')
# Print the ResultSet
for tag in result_set:
print(tag)
Output:
<div>First div</div>
Example 5: Combining Multiple Tag Lists
from bs4 import BeautifulSoup
# HTML content with div tags in different sections
html1 = "<div>Section 1</div><div>Section 1</div>"
html2 = "<div>Section 2</div><div>Section 2</div>"
soup1 = BeautifulSoup(html1, 'html.parser')
soup2 = BeautifulSoup(html2, 'html.parser')
# Create empty lists for each section
section1 = []
section2 = []
# Find div tags in the first section and extend the section1 list
section1.extend(soup1.find_all('div'))
# Find div tags in the second section and extend the section2 list
section2.extend(soup2.find_all('div'))
# Print the tags in each section
print("Section 1:")
for tag in section1:
print(tag)
print("Section 2:")
for tag in section2:
print(tag)
Example 6: Extending a List with Links
from bs4 import BeautifulSoup
# HTML content with links
html = "<a href='https://example.com'>Link 1</a><a href='https://example.org'>Link 2</a>"
soup = BeautifulSoup(html, 'html.parser')
# Create an empty list for links
link_list = []
# Find all links and extend the list with them
link_list.extend(soup.find_all('a'))
# Print the list of links
for link in link_list:
print(link)