亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

puppeteer 獲取 href 數組,然后遍歷每個 href 和該頁面上的 href

puppeteer 獲取 href 數組,然后遍歷每個 href 和該頁面上的 href

智慧大石 2022-10-27 14:11:11
我正在嘗試通過 node.js 中的 puppeteer 抓取數據目前,我正在尋找編寫一個腳本,該腳本會在 well.ca 的某個部分中抓取所有數據現在,這是我試圖通過 node.js 實現的方法/邏輯1 - 前往網站的醫學健康部分.panel-body-content2 - 使用 dom 選擇器通過 dom 選擇器獲取一組 hrefpanel-body-content a[href] 以抓取子部分3 - 使用 for 循環遍歷每個鏈接(小節)col-lg-5ths col-md-3 col-sm-4 col-xs-6 4 對于每個小節鏈接,通過獲取具有值的每個類的 href,為每個產品獲取另一個 href 數組.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]5 - 遍歷小節內的每個產品6 - 為每個產品抓取數據目前,我已經編寫了上述大部分代碼:const puppeteer = require('puppeteer');const chromeOptions = {  headless: false,  defaultViewport: null,};(async function main() {  const browser = await puppeteer.launch(chromeOptions);  try {    const page = await browser.newPage();    await page.goto("https://well.ca/categories/medicine-health_2.html");    console.log("::::::: OPEN WELL   ::::::::::");    // href attribute    const hrefs1 = await page.evaluate(      () => Array.from(        document.querySelectorAll('.panel-body-content a[href]'),       a => a.getAttribute('href')     )   );        console.log(hrefs1);    const urls = hrefs1    for (let i = 0; i < urls.length; i++) {      const url = urls[i];      await page.goto(url);    }      const hrefs2 = await page.evaluate(     () => Array.from(      document.querySelectorAll('.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]'),       a => a.getAttribute('href')     )    );當我嘗試為每個產品的每個 href 獲取一個數組時,我在數組中什么也沒有收到。如何添加嵌套的 for 循環,以獲取每個小節中每個產品的所有 href 數組,然后訪問每個產品鏈接?.col-lg-5ths col-md-3 col-sm-4 col-xs-6什么是正確的 dom 選擇器,用于獲取具有 id的類中的所有 href product_grid_link如果我想添加一個后續循環以通過每個小節中產品的 href 從每個產品中獲取信息,我該如何將其嵌入到代碼中?任何幫助將非常感激
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

似乎有些鏈接是重復的,所以最好收集最終頁面的所有鏈接,對鏈接列表進行重復數據刪除,然后刮掉最終頁面。(您也可以將最終頁面的鏈接保存在文件中以供以后使用。)該腳本收集了 5395 個鏈接(已刪除)。


'use strict';


const puppeteer = require('puppeteer');


(async function main() {

  try {

    const browser = await puppeteer.launch({ headless: false, defaultViewport: null });

    const [page] = await browser.pages();


    await page.goto('https://well.ca/categories/medicine-health_2.html');


    const hrefsCategoriesDeduped = new Set(await page.evaluate(

      () => Array.from(

        document.querySelectorAll('.panel-body-content a[href]'),

        a => a.href

      )

    ));


    const hrefsPages = [];


    for (const url of hrefsCategoriesDeduped) {

      await page.goto(url);

      hrefsPages.push(...await page.evaluate(

        () => Array.from(

          document.querySelectorAll('.col-lg-5ths.col-md-3.col-sm-4.col-xs-6 a[href]'),

          a => a.href

        )

      ));

    }


    const hrefsPagesDeduped = new Set(hrefsPages);


    // hrefsPagesDeduped can be converted back to an array

    // and saved in a JSON file now if needed.


    for (const url of hrefsPagesDeduped) {

      await page.goto(url);


      // Scrape the page.

    }


    await browser.close();

  } catch (err) {

    console.error(err);

  }

})();


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 334 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號