CSDN博客遷移至Hexo

實現思路:

  • 獲取博客列表
  • 獲取博客詳情
  • 解析博客詳情html,找出 #article_content部分
  • 通過jsoup解析博客內容,轉成md格式文件

依賴

  <dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.33</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId><version>5.8.21</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.14.3</version></dependency>

java實現代碼

package com.example.iotdbdemo.config;import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.file.FileWriter;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;import java.io.IOException;/*** @author WANG*/
@Slf4j
public class Csdn2Hexo {private static final String LIST_URL = "https://blog.csdn.net/community/home-api/v1/get-business-list?page=2&size=10000&businessType=blog&orderby=&noMore=false&year=&month=&username=%s";private static final String DETAIL_URL = "https://blog.csdn.net/%s/article/details/%s";private static final String USER_ID = "your-userId";private static String getHttp(String url) {String responseStr = null;CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpPost = new HttpGet(url);RequestConfig config = RequestConfig.custom().build();httpPost.setConfig(config);try (CloseableHttpResponse response2 = httpclient.execute(httpPost)){HttpEntity entity2 = response2.getEntity();responseStr = EntityUtils.toString(entity2);} catch (IOException e) {e.printStackTrace();}return responseStr;}private static void parseHtml(String html, String title, String description, String date) {Document document = Jsoup.parse(html);Element articleContentDiv = document.select("#article_content").first();assert articleContentDiv != null;Document doc = Jsoup.parse(articleContentDiv.html());StringBuilder markdown = new StringBuilder();markdown.append("---").append("\r\n").append("title: ").append(title).append("\r\n").append("date: ").append(date).append("\r\n").append("---").append("\r\n").append(description).append("\r\n");Element element = doc.body().children().get(0);for (Node node : element.childNodes()) {if (node instanceof TextNode textNode) {markdown.append(textNode.text());} else if (node instanceof Element childElement) {switch (childElement.tagName()) {case "h1", "h2", "h3", "h4", "h5", "h6" -> {int level = Integer.parseInt(childElement.tagName().substring(1));markdown.append("\n").append("#".repeat(level)).append(" ").append(childElement.text()).append("\n\n");}case "p" -> markdown.append("\n").append(childElement.text()).append("\n\n");case "ul" -> markdown.append(convertListToMarkdown(childElement, false));case "ol" -> markdown.append(convertListToMarkdown(childElement, true));case "pre" -> markdown.append("\n```\n").append(childElement.text()).append("\n```\n\n");default -> markdown.append("\r\n");}}}title = title.replaceAll("\\.+", "-");title = title.replaceAll(":+", "-");title = title.replaceAll("\\|+", "-");title = title.replaceAll(">+", "-");FileWriter writer = new FileWriter("E:\\gitee\\html\\" + title + ".md");writer.write(markdown.toString());log.info("Write {} success", writer.getFile().getAbsolutePath());}private static String convertListToMarkdown(Element list, boolean ordered) {StringBuilder markdown = new StringBuilder();Elements items = list.children();int count = 1;for (Element item : items) {String prefix = ordered ? (count++ + ". ") : "* ";markdown.append(prefix).append(item.text()).append("\n");}return markdown.append("\n").toString();}public static void main(String[] args) {String listUrl = String.format(LIST_URL, USER_ID);String listJson = getHttp(listUrl);JSONObject jsonObject = JSONObject.parseObject(listJson);Integer code = jsonObject.getInteger("code");if(code == 200){JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");if(CollUtil.isNotEmpty(jsonArray)){for (Object o : jsonArray) {if(o instanceof JSONObject item){Integer articleId = item.getInteger("articleId");String title = item.getString("title");String description = item.getString("description");String date = item.getString("postTime");String detailUrl = String.format(DETAIL_URL, USER_ID, articleId);String html = getHttp(detailUrl);parseHtml(html, title, description, date);}}}}}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/215309.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/215309.shtml
英文地址,請注明出處:http://en.pswp.cn/news/215309.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux實用操作篇-下篇

Linux實用操作篇-上篇&#xff1a;Linux實用操作-上篇-CSDN博客 一、網絡傳輸 1.1 ping命令 網絡是否可聯通 可以通過ping命令&#xff0c;檢查指定的網絡服務器是否是可聯通狀態 語法: ping [-c num] ip或主機名 選項&#xff1a;-c&#xff0c;檢查的次數&#xff0c;…

嵌入式SOC芯片選型

摘要&#xff1a; 本文主要探討的是如果涉及芯片選型&#xff0c;需要考慮哪些方面&#xff1f; 將相關的需求列出來&#xff0c;供后續實踐的時候參考。 SOC芯片選型 能力參數指標備注算力編碼能力VPU處理能力YUV算法資源媒體audiovideoCPU運行主頻架構DDRDDR規格DDR帶寬DD…

cmake常用設置命令及參數大全

CMake是一個跨平臺的開源構建工具,用于管理軟件項目的構建過程。它使用簡單的配置文件(CMakeLists.txt)來定義構建過程的規則。 以下是一些常用的CMake設置和命令: 1. cmake_minimum_required(VERSION x.x):指定需要的CMake版本。 2. project(project_name):設置項目的…

深入理解Java虛擬機---垃圾收集算法

深入理解Java虛擬機---垃圾收集算法 如何判定對象是否存活引用計數法可達性分析法 Java引用類型垃圾回收算法標記-清除算法復制算法標記-整理算法分代收集算法 HotSpot的算法實現枚舉根節點安全點安全區域 如何判定對象是否存活 引用計數法 引用計數算法利用額外的內存空間來…

Dockerfile創建鏡像介紹

1.介紹 Docker 提供了一種更便捷的方式&#xff0c;叫作 Dockerfile&#xff0c;docker build命令用于根據給定的Dockerfile構建Docker鏡像。 docker build語法&#xff1a; # docker build [OPTIONS] <PATH | URL | -> 常用選項說明 --build-arg&#xff0c;設置構建時的…

上海亞商投顧:滬指探底回升 AI應用方向再度爆發

上海亞商投顧前言&#xff1a;無懼大盤漲跌&#xff0c;解密龍虎榜資金&#xff0c;跟蹤一線游資和機構資金動向&#xff0c;識別短期熱點和強勢個股。 一.市場情緒 三大指數昨日探底回升&#xff0c;早盤一度均跌超1%&#xff0c;午后集體拉升翻紅&#xff0c;深成指、創業板…

FFmpeg的AVcodecParser

文章目錄 結構體操作函數支持的AVCodecParser 這個模塊是AVCodec中的子模塊&#xff0c;專門用來提前解析碼流的元數據&#xff0c;為后面的解碼做準備&#xff0c;這一點對cuda-NVdec非常明顯&#xff0c;英偉達解碼器的元數據解析是放在CPU上的&#xff0c;所以就非常依賴這個…

為什么Vue3的proxy需要Reflect呢

何為proxy Proxy 對象用于定義或修改某些操作的自定義行為&#xff0c;可以在外界對目標對象進行訪問前&#xff0c;對外界的訪問進行改寫。 var proxy new Proxy(target, handler)ES6 中的proxy目前提供了13種可代理操作攔截的行為。 何為reflect ES6 標準中&#xff0c;…

遠程工作:自由職業者如何成功賺錢

前言 在這個不斷進步的數字化時代&#xff0c;遠程工作已經從一個可選的邊緣工作方式&#xff0c;成長為主流職業趨勢的一部分。特別是自從全球疫情改變了我們的生活和工作方式以來&#xff0c;遠程工作的概念不再是遙不可及的理想&#xff0c;而是已經成為許多人日常工作的現…

SpringBoot集成swagger2配置權限認證參數

作者簡介&#xff1a;大家好&#xff0c;我是擼代碼的羊駝&#xff0c;前阿里巴巴架構師&#xff0c;現某互聯網公司CTO 聯系v&#xff1a;sulny_ann&#xff08;17362204968&#xff09;&#xff0c;加我進群&#xff0c;大家一起學習&#xff0c;一起進步&#xff0c;一起對抗…

[Java][JDK5]可變參數

我們可以假設一種情況&#xff0c;我們需要進行求和計算 在原先&#xff0c;我們考慮到參數只能傳入一個數字&#xff0c;因此我們會將需要求和的數字放在一個數組&#xff0c;傳入該數組進入方法再拆分計算 比如下面的例子:使用了增強for來對數組進行遍歷 public class Mai…

【Lidar】基于Python的三維點云數據轉二維平面+散點圖繪制

最近一直在搞點云相關的操作&#xff0c;有時候在處理點云數據時需要查看處理后的數據是否滿足需求&#xff0c;所以就想著寫一套展示點云的代碼。之前已經分享過如何可視化點云了&#xff0c;感興趣的可以自己去看下&#xff1a;【Lidar】基于Python的Open3D庫可視化點云數據。…

golang https server如何設計方便抓包定位且安全

代碼 測試 用go寫后端https服務時&#xff0c;需要定位https包中的內容是否符合預期。 有涉獵的朋友應該了解過https有一種keylog技術&#xff0c;它允許在HTTPS連接中捕獲和記錄SSL或TLS會話密鑰&#xff0c;以便于調試和分析加密流量。 本文將的就是通過可控制開啟和關閉的…

單身狗(Python)

題目描述 單身狗 “單身狗”是中文對于單身人士的一種愛稱。本題請你從上萬人的大型派對中找出落單的客人&#xff0c;以便給予特殊關愛。 輸入格式&#xff1a; 輸入第一行給出一個正整數 N&#xff08;≤50000&#xff09;&#xff0c;是已知夫妻/伴侶的對數&#xff1b;…

CPU 混合推理,非常見大模型量化方案:“二三五六” 位量化

本篇文章聊聊網上聊的比較少的具體量化操作&#xff0c;非常見整型位數的量化&#xff0c;來自讓各種開源模型能夠在 CPU 環境、CPU & GPU 環境混合推理的技術方案&#xff1a;llama.cpp 。 寫在前面 接下來&#xff0c;有計劃分享一些關于各種開源模型的實踐內容。為了能…

安裝ThingBox Eclipse Plugin

1. ChatGPT問 The latest version of the ThingBox Eclipse Plugin requires Eclipse IDE 2021-06 or later. 2. PTC官網下載 MED-61378-CD-092_F000_Eclipse-Plugin-9-0-1.zip文件, 和 MED-61098-CD-085_F000_ThingWorx-Extension-SDK-8-5-0&#xff08;需要賬號&#xff09…

虛擬化邏輯架構:KVM虛擬機通過OVS端口組實現網絡連接

目錄 一、實驗 1.CentOS 7 安裝 OpenVSwitch(構建RPM安裝包&#xff09; 2.KVM虛擬機通過OVS端口組實現網絡連接 二、問題 1.安裝openvswitch-2.5.10報錯 2.virt-install未找到命令 3.如何刪除自定義網絡 一、實驗 1.CentOS 7 安裝 OpenVSwitch(構建RPM安裝包&#xff…

字符串知識點

一. 1.字符占據一個字節 2.字符數組&#xff1a;char name[15] { W, a, n, g, L, i}; 字符串&#xff1a;char name[15] “Wang Li”&#xff1b; 二. 1.字符串&#xff1a;為有效長度&#xff08;有效字符的個數&#xff09;而不是字符數組的長度&#xff1b; 2.&#…

創建一個有陰影的threejs三維幾何體

import * as THREE from "three" import { OrbitControls } from three/examples/jsm/controls/OrbitControlsconst width window.innerWidth; const height window.innerHeight;//創建場景 const scene new THREE.Scene(); scene.background new THREE.Color(#6…

LinuxBasicsForHackers筆記 --Python 腳本基礎

添加Python模塊 Python 有一個專門用于安裝和管理 Python 包的包管理器&#xff0c;稱為 pip&#xff08;Pip Installs Packages&#xff09;。由于我們在這里使用 Python 3&#xff0c;因此您將需要 Python 3 的 pip 來下載和安裝軟件包。默認情況下應包含 Pip&#xff0c;但…