资讯专栏INFORMATION COLUMN

puppeteer 多URL爬取

focusj / 2797人阅读

摘要:基本使用返回的是一个集合需要重新遍历为了显示的图片引入了原尺寸为的图片顺序不能变启动打开监听事件跳转页面关闭顺序改变监听事件将无法监听多个的使用方法爬取数组上的所有图片,并返回其真实宽高此方法大致参考了上的答案

基本使用

</>复制代码

  1. "use strict";
  2. const puppeteer = require("puppeteer");
  3. (async () => {
  4. const browser = await puppeteer.launch();
  5. const page = await browser.newPage();
  6. let imgArr = [];
  7. page.on("domcontentloaded", async () => {
  8. imgArr = await page.$$eval("img", img => {
  9. const arr = [];
  10. // 返回的是一个集合需要重新遍历
  11. for (let i = 0; i < img.length; i++) {
  12. const obj = {
  13. width: img[i].width,
  14. naturalWidth: img[i].naturalWidth,
  15. height: img[i].height,
  16. naturalHeight: img[i].naturalHeight,
  17. isStandard: !((img[i].width * 10 <= img[i].naturalWidth || img[i].height * 10 <= img[i].naturalHeight)),
  18. url: img[i].src,
  19. level: 3,
  20. imageUrl: img[i].src,
  21. describeUrl: "",
  22. summary: `为了显示${img[i].width}x${img[i].height}的图片引入了原尺寸为${img[i].naturalWidth}x${img[i].naturalHeight}的图片`,
  23. };
  24. if (obj.width && obj.height) {
  25. arr.push(obj);
  26. }
  27. }
  28. return arr;
  29. });
  30. });
  31. await page.goto("https://www.npmjs.com/package/puppeteer", { waitUntil: "networkidle0" });
  32. await browser.close();
  33. console.log("imgArr: ", imgArr);
  34. })();

顺序不能变 :

await puppeteer.launch() 启动

await browser.newPage() 打开page

page.on 监听事件

await page.goto 跳转页面

await browser.close() 关闭

顺序改变,page.on() 监听事件将无法监听

多个URL的使用方法

爬取数组url上的所有图片,并返回其真实宽高.

</>复制代码

  1. /* eslint-disable no-undef */
  2. "use strict";
  3. const puppeteer = require("puppeteer");
  4. (async () => {
  5. const browser = await puppeteer.launch();
  6. const page = await browser.newPage();
  7. let arr = [];
  8. const html = [ "https://www.npmjs.com/package/puppeteer", "https://www.iconfont.cn/search/index?searchType=icon&q=test" ];
  9. for (let i = 0; i < html.length; i++) {
  10. await page.goto(html[i], { waitUntil: "domcontentloaded" });
  11. await page.waitForSelector("img", { timeout: 3000 });
  12. // eslint-disable-next-line no-loop-func
  13. const doms = await page.evaluate(() => {
  14. const arr = [ ...document.querySelectorAll("img") ];
  15. return arr.map(v => {
  16. return {
  17. naturalWidth: v.naturalWidth,
  18. naturalHeight: v.naturalHeight,
  19. width: v.width,
  20. height: v.height,
  21. };
  22. });
  23. });
  24. arr = [ ...arr, ...doms ];
  25. }
  26. await browser.close();
  27. })();

此方法大致参考了overflow上的答案:

Crawling multiple URL in a loop using puppeteer

Looping through a set of urls in Puppeteer

Puppeteer - Proper way to loop through multiple URLs

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/103798.html

相关文章

  • 使用Node.js爬取任意网页资源并输出高质量PDF文件到本地~

    摘要:上面只爬取了京东首页的图片内容,假设我的需求进一步扩大,需要爬取京东首页中的所有标签对应的跳转网页中的所有的文字内容,最后放到一个数组中。 showImg(https://segmentfault.com/img/bVbtVeV?w=3840&h=2160); 本文适合无论是否有爬虫以及Node.js基础的朋友观看~ 需求: 使用Node.js爬取网页资源,开箱即用的配置 将爬取到的...

    seasonley 评论0 收藏0
  • 使用Node.js爬取任意网页资源并输出高质量PDF文件到本地~

    摘要:上面只爬取了京东首页的图片内容,假设我的需求进一步扩大,需要爬取京东首页中的所有标签对应的跳转网页中的所有的文字内容,最后放到一个数组中。 showImg(https://segmentfault.com/img/bVbtVeV?w=3840&h=2160); 本文适合无论是否有爬虫以及Node.js基础的朋友观看~ 需求: 使用Node.js爬取网页资源,开箱即用的配置 将爬取到的...

    xiaoxiaozi 评论0 收藏0
  • 使用Node.js爬取任意网页资源并输出高质量PDF文件到本地~

    摘要:上面只爬取了京东首页的图片内容,假设我的需求进一步扩大,需要爬取京东首页中的所有标签对应的跳转网页中的所有的文字内容,最后放到一个数组中。 showImg(https://segmentfault.com/img/bVbtVeV?w=3840&h=2160); 本文适合无论是否有爬虫以及Node.js基础的朋友观看~ 需求: 使用Node.js爬取网页资源,开箱即用的配置 将爬取到的...

    wangym 评论0 收藏0
  • puppeteer爬虫

    摘要:爬虫爬虫又称网络机器人。每天或许你都会使用搜索引擎,爬虫便是搜索引擎重要的组成部分,爬取内容做索引。那我萌就来探讨一下网络爬虫吧。对后关于不仅仅可以用来做爬虫,因为可以编程,无头浏览器,可以用来自动化测试等等。 @(爬虫)[puppeteer|] 爬虫又称网络机器人。每天或许你都会使用搜索引擎,爬虫便是搜索引擎重要的组成部分,爬取内容做索引。现如今大数据,数据分析很火,那数据哪里来呢,...

    felix0913 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<