我有以下问题:我想抓取我正在阅读的Lightnovel的URL和标题,并将其插入我的数据库,而每个ChapterUrl都与ChapterTitle匹配
我希望最终结果在我的数据库中结构如下:
LightNovel:{
chapters:{
chapter1:{
chapterUrl: String,
chapterTitle: String
chapterUpdate: Date()
},
chapter2:{
chapterUrl: String,
chapterTitle: String
chapterUpdate: Date()
}, etc...
}
}
我想要一个像LightNovel这样的对象结构。?章节。?第1章。?章节URL到目前为止,我可以用以下代码刮取每个标题和url:
let ChapterTitle = await page.$$eval('.div class .div class .div class a', a => a.map(title=> title.textContent.trim()));
//将为我获取标题并返回一系列标题。
&;
let ChapterUrl await page.$$eval('.div class .div class .div class a', ahref => ahref.map(link => link.href));
//会给我Url并给我返回一系列链接
当我单独抓取这些东西时,我会为每个东西获得一个内容数组,当将其插入数据库时,其结构如下:
章节:对象
章节标题:数组(每个标题的数组(
chapterUrl:Array(每个链接的数组(
到目前为止还不错,我的意思是这就是我最后在代码中写的。。。
但我希望每个带标题的URL都与标题匹配,如上所述。
问题:我需要如何处理这个问题才能得到我想要的结果?
我希望我能让自己被理解,并感谢任何关于此事的回答/帮助/指导。
最好制作一个chapters
数组,并将单个章节推送到该数组中。
以下是可以做的一个版本,您可以根据需要进行修改。
请记住,我使用for
循环是因为我想让事情变得简单——你可以使用像map,filter,reduce
这样的高阶数组方法,用更少的代码行来完成类似的事情。
let lightNovel={}
let ChapterTitleArray = ['title1', 'title2']
let ChapterURLArray = ['link1', 'link2']
let chapters = []
for (let i = 0; i < ChapterTitleArray.length; i++) {
let chapter = {
chapterTitle: ChapterTitleArray[i],
chapterUrl: ChapterURLArray[i],
chapterUpdate: new Date()
}
let key= `chapter ${i+1}`
chapters.push({[key]:chapter})
}
lightNovel={chapters}
console.log(lightNovel)