获取选择器的文本,放入if语句

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

我正在尝试获取此选择器的文本(搜索结果中视频的上传日期):

#metadata-line > span:nth-child(4)

文字是这样的:“上传 x time 前”。其中的 x 是一个数字,而 time 是其中之一:分、分、秒、秒。

我想要获取文本,如果 x 小于 5,那么它会将视频链接/URL 添加到数据库中,然后进行评论。但是如果 x 大于 5,那么它将每 10 秒循环刷新一次页面,直到满足条件的视频为止。

我该怎么做?这是评论的代码的主要部分:

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.channel == true)
      {
         await page.goto('https://www.youtube.com/@username/videos');
      }
      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 autoscroll._autoScroll(page);
      }

      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
      let linked = await Promise.all((await page.$$(selector.videoTitleinSearch)).map(async a =>
      {
         return {
            url :await (await a.getProperty('href')).jsonValue(),
            title : await (await a.getProperty('title')).jsonValue()
         };
      }));
      
      let hrefs = await Promise.all((await page.$$(selector.shortsTitleinSearch)).map(async a =>
      {
         return await (await a.getProperty('href')).jsonValue();
      }));

      if (hrefs.length === 0)
      {
         linked.push(hrefs);
      }

      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]))
         {
            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());


         try
         {
            if (tweet.includes("shorts"))
            {
               await pages.goto(tweet.replace(/shorts/, "watch"));
            }
            else
            {
               // console.log(tweet);
               await pages.goto(tweet);
            }
            try
            {
               await likeVideos.likeVideos(pages);
            }
            catch (error)
            {
               console.log(error);
            }
            await pages.bringToFront();
            await pages.waitForTimeout(4000);
            await pages.evaluate(() =>
            {
               window.scrollBy(0, 550);
            });

            try
            {
               await pages.waitForSelector(selector.catchErrorInComment,
               {
                  timeout: 4000
               });
               console.log("Can't Comment");

               await pages.close();
            }
            catch
            {
               await pages.waitForSelector(selector.inputComment,
               {
                  timeout: 4000,
               });

               await pages.evaluate(() =>
               {
                  document.querySelector('#simplebox-placeholder').click();
               });
               spinners.update('comment',
               {
                  text: 'collecting comments to copy .. ',
                  color: 'yellow',
               });

               if (config.copycomment && config.ai == false) 
               {
                  await copycommnet.copyComment(pages, spinners, config);
               }else if(config.ai){
                  await aiCommented.createComments(pages , spinners ,title, config)
               }
               else if(!config.copycomment && !config.ai)
               {
                  await manualComment.manualComment(pages, spinners, config);
               }else{
                  console.log(" Check Your Configuration")
               }
               await pages.waitForTimeout(config.delay * 1000);
               await pages.close();
               spinners.succeed('comment',
               {
                  text: 'Success commenting',
                  color: 'green',
               });
               Logger.log('./logs/succesCommenting.log', config.usernamegoogle, tweet, 'success')
            }
         }
         catch (e)
         {
            await pages.close();
      
               Logger.log('./logs/errorCommenting.log', config.usernamegoogle, tweet, 'failed', e)
          
         }
         await wait(config.delay);
      }

      spinners.add('delay',
      {
         text: `wait .. we are delaying for ${config.delaycomment}s`,
         color: 'yellow',
      });
   }
   spinners.add('done',
   {
      text: `COMMENTS ADDED, FINISHING UP ..`,
      color: 'green',
   });

   

   await browser.close();
}

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

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

所以如果一个视频是在 3 分钟前上传的,它将获得数据库的链接,对于任何说“秒/秒前”的视频也是如此。但是如果没有这样的视频,那就刷新页面直到有,然后继续评论

免责声明:我只是用它来评论我自己的视频,我只是在其他视频上测试它,所以它会在我自己的频道和视频的最后阶段工作。

javascript node.js css-selectors jquery-selectors selector
© www.soinside.com 2019 - 2024. All rights reserved.