我喜欢收藏&分享书单,所以我写了一个书单配置工具。把他们制作成榜单的形式,支持配置和导出,目前的配置内容都存储在 localstorage 中的,后续有时间再考虑升级成云端存储。
![]() |
2
conge PRO 下篇读书博客时可以试试。
但感觉会在我当前工作流中增加工作量。我太懒了,未必会坚持使用... 要是能做到,把我的文章或者书籍信息扔给它,然后它自己生成图片,就好了。我有个书籍页面就是这么搞得自动生成。 https://conge.livingwithfcs.org/books/ |
![]() |
3
Honwhy OP ![]() @conge 我通过 cheerio 爬取了这个网址所有书籍,然后结合书单的数据结构 JSON ,做了一次拼装。
然后再导入进来。 ## 效果 ![]() ![]() ## 代码 ```js import fs from 'node:fs' // 导入 fs 模块来读取文件 import path from 'node:path' // 导入 path 模块来处理文件路径 import { fileURLToPath } from 'node:url' // 导入 fileURLToPath import * as cheerio from 'cheerio' import clipboardy from 'clipboardy' // 导入 clipboardy 包 import fetch from 'node-fetch' // 获取当前文件的目录名 const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) async function scrapeBooks() { const url = 'https://conge.livingwithfcs.org/books/' const resp = await fetch(url) const html = await resp.text() const $ = cheerio.load(html) const books = [] $('.hexo-douban-item').each((_, el) => { const title = $(el).find('.hexo-douban-title').text().trim() // const author = $(el).find('.book-author').text().trim() const img = $(el).find('.hexo-douban-picture>img').attr('src') const review = $(el).find('.hexo-douban-comments').text().trim() const rating = review.slice(0, 10).replace(/[^0-9.]/g, '') books.push({ title, rating, cover: img, description: review }) }) // 使用 __dirname 确保路径正确 const bookListPath = path.join(__dirname, '../public/book-lists.json') const bookListRaw = fs.readFileSync(bookListPath, 'utf8') const bookList = JSON.parse(bookListRaw) bookList[0].books = books bookList[0].name = 'conge-书单' bookList[0].qrCodeUrl = url // bookList stringify and copy to clipboard // 使用 clipboardy.write() 写入剪贴板 await clipboardy.write(JSON.stringify(bookList, null, 2)) console.log('数据已成功抓取并复制到剪贴板!🎉') } scrapeBooks() ``` ## 可惜 - 书单中缺少作者信息 - 书单的评论描述有的缺少,只有评分 - 书单的评论描述有的太长了(是否能提供更加言简意赅的精彩点评) - 书单还缺少分类,生成的图片太长了,可能会导出失败,是否可以增加分类榜单呢 |
![]() |
4
EngAPI 23 小时 15 分钟前
能不能提个需求:
我粘帖豆瓣连接,你抓取过来做成书单,这样还能坚持下去 |
![]() |
5
conge PRO @Honwhy 啊,Jason 文件我是有的。按月输出图片就好,每月的读书量也就十本上下,除了早几年的时候有过批量标记的时候。
|
![]() |
6
Honwhy OP |
![]() |
7
conge PRO 我回头研究一下
可以给每篇博客配这样的个图片,也真不错。 我的读书数据都在这里 https://github.com/conge/conge.github.io/blob/master/_data%2Fbooks.json |
![]() |
9
EngAPI 16 小时 3 分钟前
|