Spring Boot集成sitemapgen4j实现网站地图生成

embedded/2024/9/23 5:17:12/

1.什么是sitemapgen4j

是一个用于在Java中生成XML网站地图的库,比如生成网站的sitemap,如果超出了 5 万条需要写入另外一个 sitemap 当中,这个功能 sitemapgen4j 已经替我们实现了,无需担心。

 sitemap

站点地图是网站管理员向搜索引擎告知其网站上可用于抓取的页面的一种简单方法。 站点地图最简单的形式是一个 XML 文件,其中列出了站点的 URL 以及有关每个 URL 的附加元数据(上次更新时间、通常更改的频率以及相对于站点中其他 URL 的重要性) )以便搜索引擎能够更智能地抓取网站。 网络爬虫通常通过网站内的链接和其他网站发现页面。 站点地图补充了此数据,以允许支持站点地图的爬网程序拾取站点地图中的所有 URL,并使用关联的元数据了解这些 URL。 使用 Sitemap 协议并不能保证网页被搜索引擎收录,但可以为网络爬虫提供提示,以更好地爬行您的网站。 Sitemap 0.90 根据 Attribution-ShareAlike Creative Commons License 的条款提供,并得到广泛采用,包括 Google、Yahoo! 和 Microsoft 的支持。Sitemap 是网站管理员向搜索引擎通知其网站上可用页面的一种简单方法 用于爬行。 站点地图最简单的形式是一个 XML 文件,其中列出了站点的 URL 以及有关每个 URL 的附加元数据(上次更新时间、通常更改的频率以及相对于站点中其他 URL 的重要性) )以便搜索引擎能够更智能地抓取网站。

2.代码工程

实验目的:生成网站地图

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>sitemap</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.github.dfabulich</groupId><artifactId>sitemapgen4j</artifactId><version>1.1.1</version></dependency></dependencies>
</project>

application.yaml

server:port: 8088

job

package com.et.sitemap.job;import com.redfin.sitemapgenerator.SitemapIndexGenerator;
import com.redfin.sitemapgenerator.W3CDateFormat;
import com.redfin.sitemapgenerator.WebSitemapGenerator;
import com.redfin.sitemapgenerator.WebSitemapUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @author liuhaihua* @version 1.0* @ClassName SiteMapJob* @Description todo* @date 2024年04月25日 17:44*/
@Component
public class SiteMapJob {private Logger log = LoggerFactory.getLogger(getClass());//@Scheduled(cron = "0 0 0 * * ?")@Scheduled(initialDelay = 1000,fixedRate = 10000)public void generateSitemap() {log.info("start generate sitemap");String tempPath = "D://tmp/";File file = new File(tempPath);if (!file.exists()) {file.mkdirs();}String domain = "http://www.liuhaihua.cn";try {WebSitemapGenerator g1 = WebSitemapGenerator.builder(domain, file).fileNamePrefix("article").build();Date date = new Date();for (int i = 1; i < 160000; i++) {WebSitemapUrl url = new WebSitemapUrl.Options(domain + "/article/" + i).lastMod(date).build();g1.addUrl(url);}WebSitemapGenerator g2 = WebSitemapGenerator.builder(domain, file).fileNamePrefix("tag").build();Date date2 = new Date();for (int i = 1; i < 21; i++) {WebSitemapUrl url = new WebSitemapUrl.Options(domain + "/tag/" + i).lastMod(date2).build();g2.addUrl(url);}WebSitemapGenerator g3 = WebSitemapGenerator.builder(domain, file).fileNamePrefix("type").build();Date date3 = new Date();for (int i = 1; i < 21; i++) {WebSitemapUrl url = new WebSitemapUrl.Options(domain + "/type/" + i).lastMod(date3).build();g3.addUrl(url);}List<String> fileNames = new ArrayList<>();// 生成 sitemap 文件List<File> articleFiles = g1.write();articleFiles.forEach(e -> fileNames.add(e.getName()));List<File> tagFiles = g2.write();tagFiles.forEach(e -> fileNames.add(e.getName()));List<File> typeFiles = g3.write();typeFiles.forEach(e -> fileNames.add(e.getName()));// 构造 sitemap_index 生成器W3CDateFormat dateFormat = new W3CDateFormat(W3CDateFormat.Pattern.DAY);SitemapIndexGenerator sitemapIndexGenerator = new SitemapIndexGenerator.Options(domain, new File(tempPath + "sitemap_index.xml")).dateFormat(dateFormat).autoValidate(true).build();fileNames.forEach(e -> {try {// 组装 sitemap 文件 URL 地址sitemapIndexGenerator.addUrl(domain + "/" + e);} catch (MalformedURLException e1) {e1.printStackTrace();}});// 生成 sitemap_index 文件sitemapIndexGenerator.write();log.info("end generate sitemap");} catch (MalformedURLException e) {e.printStackTrace();}}
}

DemoApplication.java

java">package com.et.sitemap;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

3.测试

1f3b02483573f1ae908f5137b5844146.png

4.参考引用

  • https://github.com/dfabulich/sitemapgen4j

  • http://www.liuhaihua.cn/archives/710491.html


http://www.ppmy.cn/embedded/30305.html

相关文章

为何软件IT行业重视创新而不是稳定?

为何软件IT行业重视创新而不是稳定&#xff1f;用户为此受苦&#xff1a;用户体验差&#xff01; 彼得-蒂尔有一句名言&#xff1a;"竞争是失败者的事"。 如果没有必要&#xff0c;就不要把自己置于被迫竞争的境地。 我给年轻程序员的建议是&#xff0c;如果你想创…

【工作实践-11】关于uniapp切换账号登录失败问题

遇到问题&#xff1a;在使用uniapp写的程序中&#xff0c;第一次打开程序登录(账号密码正确)&#xff0c;调用登录接口&#xff0c;没有任何问题&#xff0c;正常登录&#xff1b;退出登陆后&#xff0c;输入不同的账号密码(账号密码均正确)&#xff0c;调用登录接口提示登陆失…

关于YOLO8学习(一)环境搭建,官方检测模型部署到手机

一&#xff0c;环境的搭建 环境 win10 python 3.11 cmake pytorch pycharm 过程 首先安装好一个pycharm&#xff0c;这里就不一一叙述了。 其次&#xff0c;选择好一个python版本&#xff0c;是关键所在。有些YOLO的版本&#xff0c;并不支持很高的python版本&#xff0c;博…

Apache Kafka知识点表格总结

之前的项目中用到RabbitMQ比较多&#xff0c;也有用到RocketMQ,&#xff0c;虽然项目中没有用到过Kafka&#xff0c;不过自己在空闲时间学习过,而且在面试中也会问到&#xff0c;因为还是有不少公司用到Kafka&#xff0c;所以做个总结&#xff0c;一方面是做为面试参考&#xf…

关于下载上传的sheetjs

一、背景 需要讲后端返回来的表格数据通过前端设置导出其中某些字段&#xff0c;而且得是xlsx格式的。 那就考虑使用控件SheetJS。如果是几年前&#xff0c;一般来说&#xff0c;保存excel的文件都是后端去处理&#xff0c;处理完成给前端一个接口&#xff0c;前端调用了打开…

(详细整理!!!!)Tensorflow与Keras、Python版本对应关系!!!

小伙伴们大家好&#xff0c;不知道大家有没有被tensorflow框架困扰过 今天我就给大家整理一下tensorflow和keras、python版本的对应关系 大家这些都可以在官网找到&#xff0c;下面我把官网的连接给大家放在这里&#xff1a;在 Windows 环境中从源代码构建 | TensorFlow (g…

使用protoc-jar-maven-plugin生成grpc项目

在《使用protobuf-maven-plugin生成grpc项目》中我们使用protobuf-maven-plugin完成了grpc代码的翻译。本文我们将只是替换pom.xml中的部分内容&#xff0c;使用protoc-jar-maven-plugin来完成相同的功能。总体来说protoc-jar-maven-plugin方案更加简便。 环境 见《使用proto…

工厂模式和策略模式区别

工厂模式和策略模式都是面向对象设计模式&#xff0c;但它们的目的和应用场景有所不同。 工厂模式是一种创建型设计模式&#xff0c;旨在通过使用一个工厂类来创建对象&#xff0c;而不是直接使用new关键字来创建对象。这样做可以使系统更容易扩展和维护&#xff0c;因为新的对…