diff --git a/bot/Douyin.mjs b/bot/Douyin.mjs index 4f17d82..be231c3 100644 --- a/bot/Douyin.mjs +++ b/bot/Douyin.mjs @@ -71,7 +71,6 @@ class Douyin extends HeroBot { await hero.close(); //删除profile文件后重试 await this.deleteProfile(); - this.ua = 'pc'; //切换到pc模式重试 return false; } @@ -116,8 +115,6 @@ class Douyin extends HeroBot { //删除profile文件后重试 await this.deleteProfile(); - //切换模式 - this.ua = this.ua == 'mob' ? 'pc' : 'mob'; } return data; diff --git a/bot/HeroBot.mjs b/bot/HeroBot.mjs index 7e7005a..49a8ac0 100644 --- a/bot/HeroBot.mjs +++ b/bot/HeroBot.mjs @@ -26,6 +26,10 @@ class HeroBot { this.ua = 'pc'; } + setMode(mode) { + this.ua = mode == 'mob' ? 'mob' : 'pc'; + } + //返回profile对象 async init(botName) { if (typeof(this.supportedBots[botName]) == 'undefined') { diff --git a/config.mjs b/config.mjs index 47123b9..9e5cdf0 100644 --- a/config.mjs +++ b/config.mjs @@ -27,6 +27,7 @@ let configs = { //bot相关配置 cloud_server: '', + default_mode: 'pc', userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', viewport: { width: 1440, diff --git a/spider.mjs b/spider.mjs index d80a30f..67f8a08 100644 --- a/spider.mjs +++ b/spider.mjs @@ -49,6 +49,7 @@ import path from 'node:path'; //spider run let spider_is_running = false, last_run_time = 0; + let last_douyin_mode = 'pc'; const task_check_time = 20; //每 20 秒抓取一次 const task_auto_run = cron.schedule(`*/${task_check_time} * * * * *`, async () => { const current_time = common.getTimestampInSeconds(); @@ -70,6 +71,7 @@ import path from 'node:path'; switch (botName) { case 'douyin': bot = new Douyin(heroCloudServer); + bot.setMode(last_douyin_mode); break; case 'kuaishou': bot = new Kuaishou(heroCloudServer); @@ -90,6 +92,11 @@ import path from 'node:path'; const data = await bot.scrap(task.url); //console.log('Data got by bot', data); + //为抖音切换模式 + if (bot.name == 'douyin') { + last_douyin_mode = last_douyin_mode == 'pc' ? 'mob' : 'pc'; + } + if (typeof(data.done) != 'undefined' && data.done == true) { task.data = data; //把抓取到的数据保存到任务里 taskMoniter.updateTask(task.id, task); diff --git a/test/scrap_test.mjs b/test/scrap_test.mjs index a9c798c..531958d 100644 --- a/test/scrap_test.mjs +++ b/test/scrap_test.mjs @@ -31,6 +31,10 @@ import getConfigs from '../config.mjs'; console.log('Hero配置', configs); const douyin = new Douyin(heroCloudServer); + + //使用手机模式,默认为pc + douyin.setMode('mob'); + console.log('请求中: %s ...', url); data = await douyin.scrap(url); console.log("解析结果:\n%s", JSON.stringify(data));