feat: add conditional check to collect matching classes
This commit is contained in:
@@ -265,8 +265,33 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
|
|||||||
const scrapedData = [];
|
const scrapedData = [];
|
||||||
|
|
||||||
while (scrapedData.length < limit) {
|
while (scrapedData.length < limit) {
|
||||||
// Get all parent elements matching the listSelector
|
let parentElements = Array.from(document.querySelectorAll(listSelector));
|
||||||
const parentElements = Array.from(document.querySelectorAll(listSelector));
|
|
||||||
|
// If we only got one element or none, try a more generic approach
|
||||||
|
if (limit > 1 && parentElements.length <= 1) {
|
||||||
|
const [containerSelector, _] = listSelector.split('>').map(s => s.trim());
|
||||||
|
const container = document.querySelector(containerSelector);
|
||||||
|
|
||||||
|
if (container) {
|
||||||
|
const allChildren = Array.from(container.children);
|
||||||
|
|
||||||
|
const firstMatch = document.querySelector(listSelector);
|
||||||
|
if (firstMatch) {
|
||||||
|
// Get classes from the first matching element
|
||||||
|
const firstMatchClasses = Array.from(firstMatch.classList);
|
||||||
|
|
||||||
|
// Find similar elements by matching most of their classes
|
||||||
|
parentElements = allChildren.filter(element => {
|
||||||
|
const elementClasses = Array.from(element.classList);
|
||||||
|
|
||||||
|
// Element should share at least 70% of classes with the first match
|
||||||
|
const commonClasses = firstMatchClasses.filter(cls =>
|
||||||
|
elementClasses.includes(cls));
|
||||||
|
return commonClasses.length >= Math.floor(firstMatchClasses.length * 0.7);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate through each parent element
|
// Iterate through each parent element
|
||||||
for (const parent of parentElements) {
|
for (const parent of parentElements) {
|
||||||
@@ -297,9 +322,15 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
|
|||||||
}
|
}
|
||||||
scrapedData.push(record);
|
scrapedData.push(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we've processed all available elements and still haven't reached the limit,
|
||||||
|
// break to avoid infinite loop
|
||||||
|
if (parentElements.length === 0 || scrapedData.length >= parentElements.length) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return scrapedData
|
}
|
||||||
};
|
return scrapedData;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user