modify puppeteer web crawl

This commit is contained in:
chungyau97
2023-07-05 17:07:45 +08:00
parent 92e50a676c
commit c18e98761a
2 changed files with 29 additions and 26 deletions
@@ -2,7 +2,7 @@ import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { TextSplitter } from 'langchain/text_splitter' import { TextSplitter } from 'langchain/text_splitter'
import { PuppeteerWebBaseLoader } from 'langchain/document_loaders/web/puppeteer' import { PuppeteerWebBaseLoader } from 'langchain/document_loaders/web/puppeteer'
import { test } from 'linkifyjs' import { test } from 'linkifyjs'
import { getAvailableURLs } from '../../../src' import { webCrawl } from '../../../src'
class Puppeteer_DocumentLoaders implements INode { class Puppeteer_DocumentLoaders implements INode {
label: string label: string
@@ -35,19 +35,20 @@ class Puppeteer_DocumentLoaders implements INode {
optional: true optional: true
}, },
{ {
label: 'Web Scrape for Relative Links', label: 'Web Crawl for Relative Links',
name: 'webScrape', name: 'boolWebCrawl',
type: 'boolean', type: 'boolean',
optional: true, optional: true,
additionalParams: true additionalParams: true
}, },
{ {
label: 'Web Scrape Links Limit', label: 'Web Crawl Links Limit',
name: 'limit', name: 'limit',
type: 'number', type: 'number',
default: 10, default: 10,
optional: true, optional: true,
additionalParams: true additionalParams: true,
description: 'Set 0 to crawl all relative links'
}, },
{ {
label: 'Metadata', label: 'Metadata',
@@ -62,7 +63,7 @@ class Puppeteer_DocumentLoaders implements INode {
async init(nodeData: INodeData): Promise<any> { async init(nodeData: INodeData): Promise<any> {
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
const metadata = nodeData.inputs?.metadata const metadata = nodeData.inputs?.metadata
const webScrape = nodeData.inputs?.webScrape as boolean const boolWebCrawl = nodeData.inputs?.boolWebCrawl as boolean
let limit = nodeData.inputs?.limit as string let limit = nodeData.inputs?.limit as string
let url = nodeData.inputs?.url as string let url = nodeData.inputs?.url as string
@@ -71,7 +72,8 @@ class Puppeteer_DocumentLoaders implements INode {
throw new Error('Invalid URL') throw new Error('Invalid URL')
} }
const puppeteerLoader = async (url: string): Promise<any> => { async function puppeteerLoader(url: string): Promise<any> {
try {
let docs = [] let docs = []
const loader = new PuppeteerWebBaseLoader(url) const loader = new PuppeteerWebBaseLoader(url)
if (textSplitter) { if (textSplitter) {
@@ -80,21 +82,22 @@ class Puppeteer_DocumentLoaders implements INode {
docs = await loader.load() docs = await loader.load()
} }
return docs return docs
} catch (err) {
if (process.env.DEBUG === 'true') console.error(`error in CheerioWebBaseLoader: ${err.message}, on page: ${url}`)
}
} }
let availableUrls: string[]
let docs = [] let docs = []
if (webScrape) { if (boolWebCrawl) {
if (!limit) limit = '10' if (process.env.DEBUG === 'true') console.info('Start Web Crawl')
availableUrls = await getAvailableURLs(url, parseInt(limit)) if (!limit) throw new Error('Please set a limit to crawl')
for (let i = 0; i < availableUrls.length; i++) { else if (parseInt(limit) < 0) throw new Error('Limit cannot be less than 0')
try { const pages: string[] = await webCrawl(url, parseInt(limit))
docs.push(...(await puppeteerLoader(availableUrls[i]))) if (process.env.DEBUG === 'true') console.info(`pages: ${JSON.stringify(pages)}, length: ${pages.length}`)
} catch (error) { for (const page of pages) {
console.error('Error loading url with puppeteer. URL: ', availableUrls[i], 'Error: ', error) docs.push(...(await puppeteerLoader(page)))
continue
}
} }
if (process.env.DEBUG === 'true') console.info('Finish Web Crawl')
} else { } else {
docs = await puppeteerLoader(url) docs = await puppeteerLoader(url)
} }
+1 -1
View File
@@ -242,7 +242,7 @@ export async function crawl(baseURL: string, currentURL: string, pages: string[]
const baseURLObj = new URL(baseURL) const baseURLObj = new URL(baseURL)
const currentURLObj = new URL(currentURL) const currentURLObj = new URL(currentURL)
if (limit !== 0) if (pages.length === limit) return pages if (limit !== 0 && pages.length === limit) return pages
if (baseURLObj.hostname !== currentURLObj.hostname) return pages if (baseURLObj.hostname !== currentURLObj.hostname) return pages