我目前正在尝试从Github的趋势页面和它们拥有的星星中获取所有趋势存储库,并从中创建一个文本文件。URL为这
我用的也是木桶。
对于存储库的列表,我做了以下操作
const data = await page.evaluate(()=>{
const tds =Array.from(document.querySelectorAll('.explore-content ol li div h3'));
return tds.map(td => td.textContent);
});给了我这样的结果
The top repositories are
charlax / professional-programming
,
ssloy / tinyraytracer
,
komeiji-satori / Dress
,
ForrestKnight / open-source-cs
,
hjacobs / kubernetes-failure-stories
,
osforscience / deep-learning-ocean
,
alexkimxyz / nsfw_data_scrapper
,
kamranahmedse / developer-roadmap
,
typescript-eslint / typescript-eslint
,
Musish / Musish
,
MisterBooo / LeetCodeAnimation
,
yagiz / Bagel
,
SpaceVim / SpaceVim
,
antonmedv / fx
,
pjialin / py12306
,
braver / programmingfonts
,
macrozheng / mall
,
Snailclimb / JavaGuide
,
schollz / howmanypeoplearearound
,
flutterchina / flutter-in-action
,
flutter / flutter
,
rikschennink / shiny
,
doocs / advanced-java
,
MFatihMAR / Awesome-Game-Networking
,
go-task / task为了得到星星,我还有一个这样的功能
const stars = await page.evaluate(()=>{
const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));
return stars.map(star=>star.textContent);
});以这种方式输出的
最高层的回复
5,304
,
379
,
,,,,,,
1,173
,
44我希望将这两个方法的输出合并到一个方法中,这样我就可以得到如下结果
查拉克斯/专业-节目有5,304颗星。
如何将data和stars方法的输出组合起来,或者如何在一个方法中完成两个不同的操作。我可以在一个map方法中执行两个模拟的操作吗?如果是的话,怎么做?
发布于 2019-01-22 22:02:45
也许是一个更安全的方法
const data = await page.evaluate(() => {
const exctactedData = [];
for (const entry of document.querySelectorAll('ol.repo-list > li')) {
exctactedData.push(`${
entry.querySelector('h3').innerText
} has ${
entry.querySelector('a[href$="/stargazers"]').innerText.trim()
} stars.`);
}
return exctactedData.join('\n');
});发布于 2019-01-22 05:59:52
您希望“压缩”两个数组的结果,然后对其进行映射。
await page.evaluate(()=>{
const repos = Array.from(document.querySelectorAll('.explore-content ol li div h3'));
const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));
// this is an array of tuples (two element arrays)
// where the first element is the name and the second is the star count
const zipped = repos.map((repoName, idx) => [repoName, stars[idx])
return zipped.map(([repoName, starCount]) => `${repoName.textContent} ${starCount.textContent}`)
});发布于 2019-01-22 06:13:41
你可以做这样的事。你不必为同一件事等两次。
const data = await page.evaluate(()=>{
const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));
const tds =Array.from(document.querySelectorAll('.explore-content ol li div h3'));
var resArr = []
for(let i = 0; i<stars.length; i++){
resArr.push(`${tds[i].textContent} has ${stars[i].textContent}`)
}
return resArr;
}https://stackoverflow.com/questions/54301991
复制相似问题