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

news/2024/10/21 3:42:16/

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/news/1451589.html

相关文章

通过城市名,实现按照A,B,C,D...排序

<script> var cities ["鞍山", "安庆", "安阳", "安顺", "安康", "阿拉尔", "安国", "阿尔山", "安达", "安丘", "安陆", "安宁", "阿…

深度学习之基于Matlab BP神经网络烟叶成熟度分类

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 一、项目背景 烟叶的成熟度是评估烟叶品质的重要指标之一&#xff0c;它直接影响着烟叶的口感、香气和理化特性。传…

题目:线性代数

问题描述&#xff1a; 解题思路&#xff1a; 列相乘&#xff0c;然后行相加。 注意点&#xff1a;由于元素数据范围最大为1e6&#xff0c;两个元素相乘乘积最大为1e12&#xff0c;如果元素类型为int则在乘的过程中就会爆炸&#xff0c;所以需要开long long类型。 AC代码…

如何在Mac上恢复格式化硬盘的数据?

“嗨&#xff0c;我格式化了我的一个Mac硬盘&#xff0c;而没有使用Time Machine备份数据。这个硬盘被未知病毒感染了&#xff0c;所以我把它格式化为出厂设置。但是&#xff0c;我忘了备份我的文件。现在&#xff0c;我想恢复格式化的硬盘驱动器并恢复我的文档&#xff0c;您能…

ASP.NET网络商店销售管理系统的设计与实现

摘 要 随着软件技术的不断进步和发展&#xff0c;信息化的管理方式越来越广泛的应用于各个领域&#xff0c;对于任何网站系统的管理来说开发一套现代化的成员管理软件是十分必要的。通过这样的软件系统&#xff0c;可以做到成员的规范管理和快速查询&#xff0c;从而减少管理…

Python爬虫:线程,进程与协程

以往的爬虫我们都采用单线程和同步的方式&#xff0c;这导致我们的爬虫及其脆弱&#xff0c;因为一点报错都会让它停下来&#xff0c;而且面对比较大的数据&#xff0c;爬虫只能选择等待&#xff0c;这种阻塞会消耗很多时间&#xff0c;为什么我们不把等待的这些时间去干别的事…

Elasticsearch索引定义

1. 前言 索引是具有相同结构的文档的集合&#xff0c;每个索引都拥有一个唯一的索引名称&#xff0c;它是ES里面非常重要的概念。一个ES集群中可以有多个索引&#xff0c;不同的索引代表不同的业务类型数据。 什么时候需要创建新的索引呢&#xff1f;一般来说有两类场景&…

[数据结构]——非比较排序—计数排序

该篇文章 所涉及代码收录仓库&#xff1a;登录 - Gitee.com 目录 1.非比较排序——计数排序 2.最终实现 1.解析 2.以int a[] { 1,3,9,1,5,1,2,3,-5,-5,-2 };为例&#xff0c;手撕分析 3.代码实现 4.计数排序具有以下主要特性&#xff1a; 1.非比较排序——计数排序 思想…