-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: #147 Set purgeOnStart: true
to process multiple sites as a server
#148
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// For more information, see https://crawlee.dev/ | ||
import { PlaywrightCrawler, downloadListOfUrls } from "crawlee"; | ||
import { Configuration, PlaywrightCrawler, downloadListOfUrls } from "crawlee"; | ||
import { readFile, writeFile } from "fs/promises"; | ||
import { glob } from "glob"; | ||
import { Config, configSchema } from "./config.js"; | ||
|
@@ -54,83 +54,89 @@ export async function crawl(config: Config) { | |
if (process.env.NO_CRAWL !== "true") { | ||
// PlaywrightCrawler crawls the web using a headless | ||
// browser controlled by the Playwright library. | ||
crawler = new PlaywrightCrawler({ | ||
// Use the requestHandler to process each of the crawled pages. | ||
async requestHandler({ request, page, enqueueLinks, log, pushData }) { | ||
const title = await page.title(); | ||
pageCounter++; | ||
log.info( | ||
`Crawling: Page ${pageCounter} / ${config.maxPagesToCrawl} - URL: ${request.loadedUrl}...`, | ||
); | ||
|
||
// Use custom handling for XPath selector | ||
if (config.selector) { | ||
if (config.selector.startsWith("/")) { | ||
await waitForXPath( | ||
page, | ||
config.selector, | ||
config.waitForSelectorTimeout ?? 1000, | ||
); | ||
} else { | ||
await page.waitForSelector(config.selector, { | ||
timeout: config.waitForSelectorTimeout ?? 1000, | ||
}); | ||
} | ||
} | ||
crawler = new PlaywrightCrawler( | ||
{ | ||
// Use the requestHandler to process each of the crawled pages. | ||
async requestHandler({ request, page, enqueueLinks, log, pushData }) { | ||
const title = await page.title(); | ||
pageCounter++; | ||
log.info( | ||
`Crawling: Page ${pageCounter} / ${config.maxPagesToCrawl} - URL: ${request.loadedUrl}...`, | ||
); | ||
|
||
const html = await getPageHtml(page, config.selector); | ||
// Use custom handling for XPath selector | ||
if (config.selector) { | ||
if (config.selector.startsWith("/")) { | ||
await waitForXPath( | ||
page, | ||
config.selector, | ||
config.waitForSelectorTimeout ?? 1000, | ||
); | ||
} else { | ||
await page.waitForSelector(config.selector, { | ||
timeout: config.waitForSelectorTimeout ?? 1000, | ||
}); | ||
} | ||
} | ||
|
||
// Save results as JSON to ./storage/datasets/default | ||
await pushData({ title, url: request.loadedUrl, html }); | ||
const html = await getPageHtml(page, config.selector); | ||
|
||
if (config.onVisitPage) { | ||
await config.onVisitPage({ page, pushData }); | ||
} | ||
// Save results as JSON to ./storage/datasets/default | ||
await pushData({ title, url: request.loadedUrl, html }); | ||
|
||
// Extract links from the current page | ||
// and add them to the crawling queue. | ||
await enqueueLinks({ | ||
globs: | ||
typeof config.match === "string" ? [config.match] : config.match, | ||
exclude: | ||
typeof config.exclude === "string" | ||
? [config.exclude] | ||
: config.exclude ?? [], | ||
}); | ||
}, | ||
// Comment this option to scrape the full website. | ||
maxRequestsPerCrawl: config.maxPagesToCrawl, | ||
// Uncomment this option to see the browser window. | ||
// headless: false, | ||
preNavigationHooks: [ | ||
// Abort requests for certain resource types | ||
async ({ request, page, log }) => { | ||
// If there are no resource exclusions, return | ||
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? []; | ||
if (RESOURCE_EXCLUSTIONS.length === 0) { | ||
return; | ||
} | ||
if (config.cookie) { | ||
const cookies = ( | ||
Array.isArray(config.cookie) ? config.cookie : [config.cookie] | ||
).map((cookie) => { | ||
return { | ||
name: cookie.name, | ||
value: cookie.value, | ||
url: request.loadedUrl, | ||
}; | ||
}); | ||
await page.context().addCookies(cookies); | ||
if (config.onVisitPage) { | ||
await config.onVisitPage({ page, pushData }); | ||
} | ||
await page.route(`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`, (route) => | ||
route.abort("aborted"), | ||
); | ||
log.info( | ||
`Aborting requests for as this is a resource excluded route`, | ||
); | ||
|
||
// Extract links from the current page | ||
// and add them to the crawling queue. | ||
await enqueueLinks({ | ||
globs: | ||
typeof config.match === "string" ? [config.match] : config.match, | ||
exclude: | ||
typeof config.exclude === "string" | ||
? [config.exclude] | ||
: config.exclude ?? [], | ||
}); | ||
}, | ||
], | ||
}); | ||
// Comment this option to scrape the full website. | ||
maxRequestsPerCrawl: config.maxPagesToCrawl, | ||
// Uncomment this option to see the browser window. | ||
// headless: false, | ||
preNavigationHooks: [ | ||
// Abort requests for certain resource types | ||
async ({ request, page, log }) => { | ||
// If there are no resource exclusions, return | ||
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? []; | ||
if (RESOURCE_EXCLUSTIONS.length === 0) { | ||
return; | ||
} | ||
if (config.cookie) { | ||
const cookies = ( | ||
Array.isArray(config.cookie) ? config.cookie : [config.cookie] | ||
).map((cookie) => { | ||
return { | ||
name: cookie.name, | ||
value: cookie.value, | ||
url: request.loadedUrl, | ||
}; | ||
}); | ||
await page.context().addCookies(cookies); | ||
} | ||
await page.route( | ||
`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`, | ||
(route) => route.abort("aborted"), | ||
); | ||
log.info( | ||
`Aborting requests for as this is a resource excluded route`, | ||
); | ||
}, | ||
], | ||
}, | ||
new Configuration({ | ||
purgeOnStart: true, | ||
}), | ||
Comment on lines
+136
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a configuration. Other differences are due to lint. |
||
); | ||
|
||
const isUrlASitemap = /sitemap.*\.xml$/.test(config.url); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did Linter change list mark?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It occurs to me that it might be due to some local configuration on your machine, @kaibadash. The linter is not configured to look at markdown files.