我在一个正在做的项目中使用了metascraper。我正在将自定义规则传递给构造器。它实际上是从页面中抓取实际内容。问题是,它似乎找到了与CSS选择器匹配的每个标记,并组合了页面上每个标记的所有text()内容。我查看了metascraper网站和github,没有找到任何关于改变这种模式/行为的选项的信息。我确保每个抓取请求都会创建一个新的metascraper实例,以防它只是在对象的多次使用中使用相同的成员变量,但这似乎没有任何作用。有什么想法吗?
编辑:另外,理想情况下,metascraper将返回它找到的选择器集合的数组数组。我在一个组中有4个选择器,它们在整个页面中以组的形式出现。我需要它按顺序迭代选择器,直到它找不到第一个选择器的任何实例(也就是组已经不再出现在页面上)。
type4: async (page: Page): Promise<Extract[]> => {
const html = await page.content()
const url = await page.url()
const type4MetascraperInstance = createType4MetaScraperInstance()
const metadata = await type4MetascraperInstance({ html: html, url: url })
console.log('metadata: ', metadata)
const extract: Extract[] = [{
fingerprint: 'type4',
author: metadata.author,
body: metadata.description,
images: null,
logo: null,
product: null,
rating: null,
title: metadata.title,
videos: null
}]
return extract
}创建Type4 metascraper实例的函数为:
function createType4MetaScraperInstance() {
const toDescription = toRule(description)
const toAuthor = toRule(author)
const toTitle = toRule(title, { removeSeparator: false })
const type4MetaScraperInstance = metaScraper([ {
author: [
toAuthor($ => $('.a-profile-name').text()),
],
title: [
toTitle($ => $('a[data-hook="review-title"] > span').text()),
],
description: [
toDescription($ => $('.review-text-content').text()),
]
} ])
return type4MetaScraperInstance
}发布于 2020-09-27 05:25:04
我决定在这里设计一个不同的解决方案,它使用python脚本来正确地解析评论,它将需要对google云数据存储进行读/写。人们提供的一些建议是编写我自己的对cheeriojs (https://cheerio.js.org/)的调用,而不是使用元爬行器。
https://stackoverflow.com/questions/64036329
复制相似问题