await crawler.run([
{ url: 'http://quotes.toscrape.com/api/quotes?page=1', label: 'quotes' },
])
// ...
routerRef.addHandler('quotes', async (context) => {
const { request, sendRequest, enqueueLinks, response, body, log } = context
log.info(request.url)
if (!(body instanceof Buffer)) {
log.error(`Expected a Buffer instance.`)
return
}
const json: QuotesResponse = JSON.parse(body.toString())
if ((json.quotes?.length ?? 0) > 0) {
await datasetRef.pushData(json)
}
if (json.has_next) {
const urls = [`http://quotes.toscrape.com/api/quotes?page=${json.page + 1}`]
await enqueueLinks({
urls,
label: 'quotes',
strategy: EnqueueStrategy.All,
})
} else {
log.warning('No next was found')
}
})
await crawler.run([
{ url: 'http://quotes.toscrape.com/api/quotes?page=1', label: 'quotes' },
])
// ...
routerRef.addHandler('quotes', async (context) => {
const { request, sendRequest, enqueueLinks, response, body, log } = context
log.info(request.url)
if (!(body instanceof Buffer)) {
log.error(`Expected a Buffer instance.`)
return
}
const json: QuotesResponse = JSON.parse(body.toString())
if ((json.quotes?.length ?? 0) > 0) {
await datasetRef.pushData(json)
}
if (json.has_next) {
const urls = [`http://quotes.toscrape.com/api/quotes?page=${json.page + 1}`]
await enqueueLinks({
urls,
label: 'quotes',
strategy: EnqueueStrategy.All,
})
} else {
log.warning('No next was found')
}
})