当前位置: 首页 > news >正文

生成网站sitemap.xml地图教程

要生成 sitemap.xml 文件,需要通过爬虫程序抓取网站的所有有效链接。以下是完整的解决方案:


步骤 1:安装必要的 Python 库

  • ounter(line
pip install requests beautifulsoup4 lxml

步骤 2:创建 Python 爬虫脚本 (sitemap_generator.py)

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
import requestsfrom bs4 import BeautifulSoupfrom urllib.parse import urljoin, urlparseimport xml.etree.ElementTree as ETfrom datetime import datetime
def get_all_links(base_url):    # 存储已访问和待访问的链接    visited = set()    queue = [base_url]    all_links = set()
    while queue:        url = queue.pop(0)        if url in visited:            continue
        try:            headers = {                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'            }            response = requests.get(url, headers=headers, timeout=10)            if response.status_code != 200:                continue
            # 添加到已访问列表            visited.add(url)            all_links.add(url)            print(f"Crawled: {url}")
            # 解析 HTML 获取新链接            soup = BeautifulSoup(response.text, 'lxml')            for link in soup.find_all('a', href=True):                href = link['href'].strip()                full_url = urljoin(url, href)
                # 过滤无效链接                parsed = urlparse(full_url)                if parsed.scheme not in ('http', 'https'):                    continue                if not parsed.netloc.endswith('91kaiye.cn'):  # 仅限本站链接                    continue                if '#' in full_url:  # 忽略锚点                    full_url = full_url.split('#')[0]
                # 添加到待访问队列                if full_url not in visited:                    queue.append(full_url)
        except Exception as e:            print(f"Error crawling {url}: {str(e)}")
    return all_links
def create_sitemap(links, filename='sitemap.xml'):    root = ET.Element('urlset', xmlns='http://www.sitemaps.org/schemas/sitemap/0.9')        for link in sorted(links):        url_elem = ET.SubElement(root, 'url')        ET.SubElement(url_elem, 'loc').text = link        ET.SubElement(url_elem, 'lastmod').text = datetime.now().strftime('%Y-%m-%d')        ET.SubElement(url_elem, 'changefreq').text = 'daily'        ET.SubElement(url_elem, 'priority').text = '0.8'        tree = ET.ElementTree(root)    tree.write(filename, encoding='utf-8', xml_declaration=True)    print(f"\nSitemap generated: {filename} with {len(links)} URLs")
if __name__ == '__main__':    base_url = 'https://www.91kaiye.cn/'    print("Starting crawl...")    links = get_all_links(base_url)    create_sitemap(links)

步骤 3:运行脚本

  • ounter(line
python sitemap_generator.py

执行说明:

  1. 爬虫逻辑

    • 从首页 https://www.91kaiye.cn/ 开始广度优先搜索

    • 自动过滤非本站链接、锚点和无效 URL

    • 记录每个页面的最后修改日期(默认当天)

    • 设置更新频率为 daily,优先级为 0.8

  2. 输出文件

    • 生成的 sitemap.xml 格式如下:
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      • ounter(line
      <?xml version='1.0' encoding='utf-8'?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">  <url>    <loc>https://www.91kaiye.cn/page1</loc>    <lastmod>2023-10-05</lastmod>    <changefreq>daily</changefreq>    <priority>0.8</priority>  </url>  ...</urlset>

注意事项:

  1. 反爬措施

    • 如果网站有反爬机制,可能需要:
      • 添加 time.sleep(1) 延迟请求

      • 使用代理 IP

      • 设置更真实的请求头

  2. 动态内容

    • 对于 JavaScript 渲染的页面(如 Vue/React),需改用 SeleniumPlaywright

  3. 优化建议

    • 在服务器上定期运行(如每周一次)

    • 提交到 Google Search Console

    • robots.txt 中添加:
      • ounter(line
      Sitemap: https://www.91kaiye.cn/sitemap.xml

替代方案:使用在线工具

如果不想运行代码,可用在线服务生成:

  1. XML-Sitemaps.com

  2. Screaming Frog SEO Spider(桌面工具)


生成后请将 sitemap.xml 上传到网站根目录,并通过百度/Google站长工具提交。

http://www.lryc.cn/news/611604.html

相关文章:

  • 从代码学习LLM - llama3 PyTorch版
  • GitHub Spark公共预览版上线
  • 利用OJ判题的多语言优雅解耦方法深入体会模板方法模式、策略模式、工厂模式的妙用
  • 本地服务器端部署基于大模型的通用OCR项目——dots.ocr
  • 达梦数据库日常运维命令
  • cdn是什么
  • 【C++】unordered系列容器使用及封装
  • 生成式 AI 重塑自动驾驶仿真:4D 场景生成技术的突破与实践
  • QT----不同线程中信号发送了槽函数没反应QObject::connect: Cannot queue arguments of typeXXX
  • SG105 Pro 网管交换机的3种VLAN配置
  • java实现生成自定义二维码
  • 软考信息安全工程师11月备考
  • Ragflow介绍与安装
  • 考研408_数据结构笔记(第四章 串)
  • Spearman 相关系数与 Pearson 相关系数的区别
  • Java 工具类的“活化石”:Apache Commons 核心用法、性能陷阱与现代替代方案
  • 湖南14个市州分流线得分率对比
  • 【科研绘图系列】R语言绘制瀑布图
  • RNN梯度爆炸/消失的杀手锏——LSTM与GRU
  • 自学嵌入式 day45 ARM体系架构
  • 异世界历险之数据结构世界(非递归快排,归并排序(递归,非递归))
  • Leetcode题解:739每日温度,用单调栈解决问题!
  • 分布式存储 Ceph 的演进经验 · SOSP 2019
  • 从零搭建React框架--第一章:create-react-app、antd、less
  • 深度解析|资源位管理工具如何重构媒体商业化效率?
  • 《算法导论》第 8 章—线性时间排序
  • 福彩双色球第2025090期篮球号码分析
  • 【STL源码剖析】从源码看 vector:底层扩容逻辑与内存复用机制
  • Python实现信号小波分解与重构
  • 飞算JavaAI开发平台:重构开发全流程——从需求到工程的智能化跃迁