Generate sitemap

To create an XML file for your sitemap using Python, you can use the built-in "xml.etree.ElementTree" module. Here's an example of how you can modify the previous script to generate an XML sitemap:

import requests
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from datetime import datetime

def get_links(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    links = [a.get('href') for a in soup.find_all('a', href=True)]
    return links

def generate_sitemap_xml(url, output_file='sitemap.xml'):
    links = get_links(url)

    # Create the root element
    root = ET.Element("urlset")
    root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")

    # Add each link as a child element with additional information
    for link in links:
        url_element = ET.SubElement(root, "url")

        loc_element = ET.SubElement(url_element, "loc")
        loc_element.text = link

        lastmod_element = ET.SubElement(url_element, "lastmod")
        lastmod_element.text = datetime.now().isoformat()

        changefreq_element = ET.SubElement(url_element, "changefreq")
        changefreq_element.text = "daily"  # You can adjust this value based on your needs

        priority_element = ET.SubElement(url_element, "priority")
        priority_element.text = "0.8"  # You can adjust this value based on your needs

    # Create an ElementTree object and write to the XML file
    tree = ET.ElementTree(root)
    tree.write(output_file, encoding='utf-8', xml_declaration=True)

# Replace 'your_url_here' with the actual URL you want to generate a sitemap for
generate_sitemap_xml('your_url_here')

This script will create an XML file named sitemap.xml in the same directory as your script. You can customize the output file name by providing a different value for the output_file parameter in the generate_sitemap_xml function.