后端python,前端html,如何实现联动?

今天我作为技术小白,讲解一下成功案例。

背景

为了平时收集sitemap(网站地图)比较方便,突发奇想,编写一个生成sitemap的程序。

案例

sitemap网站地图生成器

本案例,采用flask框架进行网站搭建。

1.准备源码文件

宝塔面板:9.4.0
文件路径结构:
项目文件夹
├─ app.py
└─ templates
└─ index.html

文件级,总共用到两个文件,app.py和templates/index.html(注意app.py和templates是平级)。

前端代码(templates/index.html):

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>Sitemap 生成器</title>
</head>

<body>
    <h1>Sitemap 生成器</h1>
    <form method="post" action="/">
        <label for="url">请输入网址:</label><br>
        <input type="text" id="url" name="url" required><br>
        <input type="checkbox" id="include_subpages" name="include_subpages"> 包含子页面<br>
        <input type="submit" value="生成 Sitemap">
    </form>
</body>
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//tongji.7hl.cn/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '3']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

</html>

PYTHON代码(app.py):

from flask import Flask, render_template, request, send_file
import requests
from requests.exceptions import RequestException
from bs4 import BeautifulSoup
import tempfile
import uuid
import os
from urllib.parse import urljoin
import validators

app = Flask(__name__)


def get_all_links(url, include_subpages=True):
    all_links = []
    try:
        response = requests.get(url)
        response.raise_for_status()  # 检查响应状态码,如果不是 200 会抛出异常
        soup = BeautifulSoup(response.text, 'html.parser')
        links = [link.get('href') for link in soup.find_all('a')]
        absolute_links = [urljoin(url, link) for link in links if link]
        all_links.extend(absolute_links)

        if include_subpages:
            for link in all_links.copy():
                if link.startswith(url):
                    try:
                        subpage_response = requests.get(link)
                        subpage_response.raise_for_status()
                        subpage_soup = BeautifulSoup(subpage_response.text, 'html.parser')
                        subpage_links = [sub_link.get('href') for sub_link in subpage_soup.find_all('a')]
                        subpage_absolute_links = [urljoin(link, sub_link) for sub_link in subpage_links if sub_link]
                        all_links.extend(subpage_absolute_links)
                    except RequestException as e:
                        print(f"请求子页面 {link} 时出错: {e}")
    except RequestException as e:
        print(f"请求 {url} 时出错: {e}")
    return all_links


def generate_sitemap(links):
    xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
    xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
    for link in links:
        xml += '  <url>\n'
        xml += f'    <loc>{link}</loc>\n'
        xml += '  </url>\n'
    xml += '</urlset>'
    return xml


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        url = request.form.get('url')
        if not validators.url(url):
            return "输入的网址无效,请重新输入。"
        include_subpages = request.form.get('include_subpages') == 'on'
        links = get_all_links(url, include_subpages)
        sitemap_xml = generate_sitemap(links)

        # 创建临时文件保存 Sitemap
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xml')
        try:
            temp_file.write(sitemap_xml.encode('utf-8'))
            temp_file.close()
            return send_file(temp_file.name, as_attachment=True, download_name=f'sitemap_{str(uuid.uuid4())}.xml',
                             mimetype='application/xml')
        finally:
            os.remove(temp_file.name)  # 确保文件在使用后被删除

    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

2.宝塔创建python项目

1)新建python项目

2025-03-12T04:34:15.png

2)设置启动方式

设置启动方式“gunicorn”
2025-03-12T04:38:40.png

3)安装模块(依赖包)

安装以下模块(依赖包)
Flask, render_template, request, send_file,RequestException,BeautifulSoup,tempfile,uuid,os,urljoin,validators
2025-03-12T04:39:50.png
2025-03-12T04:43:55.png
点击安装。

4)启动项目

2025-03-12T04:45:17.png

成品效果

2025-03-12T04:46:55.png
点击“生成Sitemap”后,等待生成结果,自动下载sitemap.xml文件。

分类: python源码网站建设 标签: sitemappythonhtmlflask网站地图

评论

暂无评论数据

暂无评论数据

目录