from bs4 import BeautifulSoup
import csv
# The HTML code containing the <a> tags
html_code = '''
<!DOCTYPE html>
<html>
<head>
<title>Sample HTML</title>
</head>
<body>
<div>
<a href="https://www.example.com/page1">Link 1</a>
<a href="https://www.example.com/page2">Link 2</a>
<a href="https://www.example.com/page3">Link 3</a>
</div>
</body>
</html>
'''
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html_code, 'html.parser')
# Find all the <a> tags within the <div> tag
div_tag = soup.find('div')
a_tags = div_tag.find_all('a')
# Create a list to store the data
data = []
# Extract information from the <a> tags and store in the list
for a_tag in a_tags:
link_text = a_tag.text
link_url = a_tag['href']
data.append((link_text, link_url))
# Define the CSV file name
csv_file = 'a_tags_data.csv'
# Write the data to the CSV file
with open(csv_file, 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Link Text', 'Link URL']) # Write header
writer.writerows(data) # Write the data rows
print(f'Data has been successfully stored in {csv_file}.')