用 puppeteer returns 抓取 table 错误的结果
Scraping table with puppeteer returns wrong results
我正在尝试抓取 (this) 产品页面,特别是当您点击“查看所有出价”时显示的模式。
html 结构只是一个简单的 table,我正在尝试获取每个“Size”元素。问题是,每当我 运行 我的代码时,它都会打开模式,但只有 returns 一些随机的鞋码不按顺序。
示例:
shoeSizeBids: [
'14', '11.5', '10.5',
'11', '8.5', '11',
'9', '9', '7',
'13'
]
我的代码:
const bidsChartSel =
'#market-summary > div.ask.ask-button-b > div.sale-size > div:nth-child(2)';
await Promise.all([page.click(bidsChartSel)]);
// Get all the shoe size bids
const shoeSizeBids= await page.evaluate(() =>
Array.from(
document.querySelectorAll('tbody > tr > td:nth-child(1)'),
(element) => element.textContent
)
);
排序顺序来自该页面,即尺寸按该顺序呈现。要正确排序它们,您需要:
- (可选)去除重复尺寸
- 将字符串数组转换为浮点数数组
- 对数组进行排序
可以通过以下方式实现:
const uniqueSortedSizes = Array.from(new Set(shoeSizeBids))
.map(s => parseFloat(s, 10))
.sort((a, b) => a > b ? 1: a < b ? -1 : 0);
您正在用当前选择器 (tbody > tr > td:nth-child(1)
) 匹配多个 HTML 个表。模态里面的一个使用:
.activity-table > tbody > tr > td:nth-child(1)
您还可以将 page.$$eval
用作 shorthand 的人偶 Array.from(document.querySelectorAll(selector))
:
const shoeSizeBids = await page.$$eval('.activity-table > tbody > tr > td:nth-child(1)', elems => elems.map(el => el.innerText))
我正在尝试抓取 (this) 产品页面,特别是当您点击“查看所有出价”时显示的模式。
html 结构只是一个简单的 table,我正在尝试获取每个“Size”元素。问题是,每当我 运行 我的代码时,它都会打开模式,但只有 returns 一些随机的鞋码不按顺序。
示例:
shoeSizeBids: [
'14', '11.5', '10.5',
'11', '8.5', '11',
'9', '9', '7',
'13'
]
我的代码:
const bidsChartSel =
'#market-summary > div.ask.ask-button-b > div.sale-size > div:nth-child(2)';
await Promise.all([page.click(bidsChartSel)]);
// Get all the shoe size bids
const shoeSizeBids= await page.evaluate(() =>
Array.from(
document.querySelectorAll('tbody > tr > td:nth-child(1)'),
(element) => element.textContent
)
);
排序顺序来自该页面,即尺寸按该顺序呈现。要正确排序它们,您需要:
- (可选)去除重复尺寸
- 将字符串数组转换为浮点数数组
- 对数组进行排序
可以通过以下方式实现:
const uniqueSortedSizes = Array.from(new Set(shoeSizeBids))
.map(s => parseFloat(s, 10))
.sort((a, b) => a > b ? 1: a < b ? -1 : 0);
您正在用当前选择器 (tbody > tr > td:nth-child(1)
) 匹配多个 HTML 个表。模态里面的一个使用:
.activity-table > tbody > tr > td:nth-child(1)
您还可以将 page.$$eval
用作 shorthand 的人偶 Array.from(document.querySelectorAll(selector))
:
const shoeSizeBids = await page.$$eval('.activity-table > tbody > tr > td:nth-child(1)', elems => elems.map(el => el.innerText))