@
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()
```
## 可惜
- 书单中缺少作者信息
- 书单的评论描述有的缺少,只有评分
- 书单的评论描述有的太长了(是否能提供更加言简意赅的精彩点评)
- 书单还缺少分类,生成的图片太长了,可能会导出失败,是否可以增加分类榜单呢