TIP
不知道为什么百度收录那么慢,提交sitemap已经一周了,貌似只有个主页,相反谷歌却已经有十来个链接了,再尝试每天API提交一次试试。
# 简述
百度提供了相应的API收录提交接口,不知道作用大不大,反正试一下吧,sitemap提交了这么多天也没有动静,也没有更新。
sitemap将网站所有的链接都生成了,但是有一些链接是不太友好的,例如文章的链接,就要是文章的链接,不能标签,分类的链接也存在,打开之后,并不能直接看到文章的内容,还得要点一次文章,这个是不太好的。所以当用API提交时,应该排除掉自己不需要的链接,首先要拿到sitemap的内容,然后通过正则筛选到自己想要的链接,接着提交到API就行了。
例如,存在以下几种链接
https://srcrs.top/posts/2019/08/12/%E9%AB%98%E7%B2%BE%E5%BA%A6%E5%BF%AB%E9%80%9F%E5%B9%82%E6%A8%A1%E6%9D%BF.html
https://srcrs.top/posts/tags/bfs.html
https://srcrs.top/posts/categories/Note.html
只需要用一个正则公式就能够获取到自己想要的链接
https://srcrs.top/posts/[\d]{4}/[\d]{2}/[\d]{2}/.+.html
把这些链接组合成一个字符串,官网给的示例,我还是有点看不懂,post发送方式只会携带键值对的参数,看到别人的实现方式就是类似当作socket发送消息的方式,还是挺新奇的。索性就当作一个工具类拿来用了。
# 所需jar包
使用maven建立的一个项目,需要导入jsoup的包。
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
# 代码
public class App {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("sitemap.xml地址").get();
String str = doc.toString();
String pattern = "https://srcrs.top/posts/[\\d]{4}/[\\d]{2}/[\\d]{2}/.+.html";
Pattern regex = Pattern.compile(pattern);
Matcher m = regex.matcher(str);
String json = "";
while (m.find()) {
json = json + m.group() + "\n";
}
String url = "接口调用地址";
System.out.println(App.postTuiSong(url,json));
}
/**
* 发送请求的工具类
* @param url
* @param Parameters
* @return
*/
public static String postTuiSong(String url, String Parameters) {
String PostUrl = url;
if (null == PostUrl || Parameters.length() == 0) {
return null;
}
String result = "";
PrintWriter out = null;
BufferedReader in = null;
try {
//建立URL之间的连接
URLConnection conn = new URL(PostUrl).openConnection();
//设置通用的请求属性
conn.setRequestProperty("User-Agent", "curl/7.12.1");
conn.setRequestProperty("Host", "data.zz.baidu.com");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("Content-Length", "83");
//发送POST请求必须设置如下两行
conn.setDoInput(true);
conn.setDoOutput(true);
//获取conn对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数
out.print(Parameters.trim());
//进行输出流的缓冲
out.flush();
//通过BufferedReader输入流来读取Url的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
//}
} catch (Exception e) {
System.out.println("post推送出现异常!" + e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
System.out.println("post推送结果:" + result);
return result;
}
}
# maven.yml
github actions的运行配置文件
name: Java CI with Maven
on:
push:
schedule:
- cron: '0 22 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
env:
URL_1: ${{ secrets.URL_1 }}
URL_2: ${{ secrets.URL_2 }}
run: |
cd bilibili
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get -f install
unzip chromedriver_linux64.zip
mvn compile exec:java -Dexec.mainClass="org.example.App" -Dexec.args="${URL_1} ${URL_2}"