Skip to content
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

feat: Customizing selector can sometimes cause web crawlers to fail #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,28 @@ export async function crawl(config: Config) {
);

// Use custom handling for XPath selector
if (config.selector) {
if (config.selector.startsWith("/")) {
await waitForXPath(
page,
config.selector,
config.waitForSelectorTimeout ?? 1000,
let effectiveSelector = config.selector;
if (effectiveSelector) {
try {
if (effectiveSelector.startsWith("/")) {
await waitForXPath(
page,
effectiveSelector,
config.waitForSelectorTimeout ?? 1000,
);
} else {
await page.waitForSelector(effectiveSelector, {
timeout: config.waitForSelectorTimeout ?? 1000,
});
}
} catch (error) {
console.log(
`Selector "${config.selector}" not found. Defaulting to <body>.`,
);
} else {
await page.waitForSelector(config.selector, {
timeout: config.waitForSelectorTimeout ?? 1000,
});
effectiveSelector = undefined;
}
}

const html = await getPageHtml(page, config.selector);

const html = await getPageHtml(page, effectiveSelector);
// Save results as JSON to ./storage/datasets/default
await pushData({ title, url: request.loadedUrl, html });

Expand Down
Loading