Selector Href not identified

问题描述 投票:0回答:0

我有这段代码可以转到关键字的 YouTube 链接,找到不到 5 分钟前上传的视频,并对它们发表评论。

然而,问题是检查选择器 href 的“上传 x mins/secs 前”的循环不起作用,当有那些上传时间的视频时,它只是刷新页面。

我没有尝试任何东西,因为我想不出任何东西。我想要一些帮助,这真的让我很烦。

以下是我需要帮助的代码。

await page.goto('https://www.youtube.com', wait)

console.log('=========== Start Commenting ==============');

spinners.add('first-spinner', {
  text: 'Searching for videos..',
  color: 'yellow',
});

for (let i = 0; i < keyword.length; i++) {
  if (config.uservideo == true) {
    await page.goto('https://www.youtube.com/@username');
  } else {
    await page.goto(`https://www.youtube.com/results?search_query=${keyword[i]}&sp=CAISBAgBEAE%253D&sp=EgIQAQ%253D%253D`);
    const element = await page.$(selector.shortvideos);
    if (element) {
      await page.evaluate(() => {
        document.querySelector(selector.shortvideos).remove();
      });
    }
  }

  await page.waitForTimeout(3000);
  spinners.succeed('first-spinner', {
    text: 'done..',
    color: 'green'
  });
  await page.waitForTimeout(7000);
  spinners.add('hasil', {
    text: 'Collecting videos..',
    color: 'yellow'
  });

  // collecting links
  // collecting links
  let linked = [];
  while (linked.length === 0) {
    linked = await Promise.all((await page.$$(selector.videoTitleinSearch)).map(async a => {
      const uploadedago = await a.$(selector.uploadedago);
      if (uploadedago) {
        const href = await uploadedago.getProperty('href').jsonValue();
        if (href &&
          (href.includes("5 minutes ago") ||
            href.includes("4 minutes ago") ||
            href.includes("3 minutes ago") ||
            href.includes("2 minutes ago") ||
            href.includes("1 minute ago") ||
            href.includes(" seconds ago") ||
            href.includes(" second ago"))
        ) {
          return {
            url: await a.getProperty('href').jsonValue(),
            title: await a.getProperty('title').jsonValue()
          };
        }
      }
      return null; // add this line to return null if uploadedago is not found
    }));
    linked = linked.filter(Boolean); // add this line to remove null values from linked array
    if (linked.length === 0) {
      console.log("No videos found, refreshing page...");
      await page.reload({
        waitUntil: "networkidle2"
      });
      await page.waitForTimeout(5000);
    }
    const link = linked.filter(el => el.url != null);

    spinners.succeed('hasil', {
      text: `FOUND ${link.length} LINKS`,
      color: 'green',
    });

    for (i in link) {
      if (readLog().includes(link[i].url)) {
        spinners.add('already', {
          text: 'already commented on video..',
          color: 'red',
        });
        continue;
      }

      spinners.add('comment', {
        text: 'commenting on video..',
        color: 'yellow',
      });
      const tweet = link[i].url;
      const title = link[i].title;
      const pages = await browser.newPage();
      await pages.setViewport({
        width: 1366,
        height: 768
      });
      await pages.setUserAgent(randomUserAgent.UA());

      if (tweet.includes("shorts")) {
        await pages.goto(tweet.replace(/shorts/, "watch"));
      } else {
        await pages.goto(tweet);
      }
      try {
        await likeVideos.likeVideos(pages);
        await commentVideos.commentVideos(pages, config.message);
        addToLog(link[i].url);
        spinners.succeed('comment', {
          text: 'commented on video..',
          color: 'green',
        });
        console.log(`Commented on video "${title}"`);
      } catch (error) {
        console.log(`Error while commenting on video "${title}": ${error}`);
        spinners.fail('comment', {
          text: 'commenting failed',
          color: 'red',
        });
      }
      await pages.close();
      await page.waitForTimeout(15000);
    }
  }
}
}


console.log('COMMENTED ..');


startApp(config, Config(paths, config, executablePath("chrome")));

function readLog() {
  const data = fs.readFileSync('./logs/succesCommenting.log', 'utf8');
  return data;
}

免责声明:我仅将此用于我自己的频道,我只是在努力上传视频之前使用其他视频来验证一切是否正常,只是为了找出不正常的地方;所以这更容易测试。

javascript node.js puppeteer href
© www.soinside.com 2019 - 2024. All rights reserved.