netnr 2024-12-11

监测方法

/// <summary>
/// 开播
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string Live(string url)
{
    var chromedriver = IndexMap.Configuration["Selenium:chromedriver"];

    // 设置 ChromeDriver 的路径
    var chromeDriverService = string.IsNullOrWhiteSpace(chromedriver)
        ? ChromeDriverService.CreateDefaultService()
        : ChromeDriverService.CreateDefaultService(chromedriver);

    var chromeOptions = new ChromeOptions();
    var binaryLocation = IndexMap.Configuration["Selenium:BinaryLocation"];
    if (!string.IsNullOrWhiteSpace(binaryLocation))
    {
        chromeOptions.BinaryLocation = binaryLocation;
    };
    var arguments = IndexMap.Configuration.GetSection("Selenium:Arguments").Get<string[]>();
    if (arguments?.Length > 0)
    {
        chromeOptions.AddArguments(arguments);
    }

    // 启动 Chrome 浏览器
    using var chromeInstance = new ChromeDriver(chromeDriverService, chromeOptions);

    // 导航到目标网页
    // 电影频道央影传媒 https://live.douyin.com/208823316033
    chromeInstance.Navigate().GoToUrl(url);

    var sleep = IndexMap.Configuration.GetSection("Selenium:douyin/live:Sleep").Get<int>();
    Thread.Sleep(sleep == 0 ? 2000 : sleep);

    // 执行 JavaScript 脚本
    var es = IndexMap.Configuration["Selenium:douyin/live:ExecuteScript"];
    var result = chromeInstance.ExecuteScript(es);

    // 关闭浏览器
    chromeInstance.Quit();

    return result.ToString();
}

配置信息

appsettings.json

{
  //Selenium 配置
  "Selenium": {
    //驱动路径,需要版本匹配、执行权限
    "chromedriver": "/package/site/selenium/chromedriver",
    //浏览器路径,google-chrome --version 查看版本号,驱动和浏览器版本需要匹配(大版本号)
    "BinaryLocation": "/opt/google/chrome/google-chrome",
    //浏览器参数
    "Arguments": [
      "--remote-debugging-port=9222",
      "--headless",
      "--no-sandbox",
      "--disable-gpu",
      "--disable-sync",
      "--disable-translate",
      "--disable-extensions",
      "--disable-default-apps",
      "--disable-dev-shm-usage",
      "--disable-software-rasterizer",
      "--disable-background-networking",
      "--disable-features=NetworkService,NetworkServiceInProcess"
    ],

    "douyin/live": {
      "Sleep": 2000,
      // 存在 video 标签则视为开播
      "ExecuteScript": "return document.querySelector('video')!=null"
    }
  }
}

安装 Chrome

无 GUI ,命令行安装

先配置源 /etc/yum.repos.d/google-chrome.repo

[google-chrome]
name=google-chrome
baseurl=https://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub
# 清理并更新 DNF 缓存
dnf clean all
dnf makecache

# 安装稳定版
dnf install google-chrome-stable

# 查看版本号
google-chrome --version
# 安装目录在 /opt/google/chrome

FAQ

依赖的包

<PackageReference Include="Selenium.WebDriver" Version="4.27.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="131.0.6778.8700" />

ChromeDriver 需要和安装的浏览器大版本号匹配
Windows 系统可不显示引入 Selenium.WebDriver.ChromeDriver ,使用默认会自动下载对应驱动
Linux 测试有问题,所以用了指定参数

没涉及到用户登录、Cookie ,同时监测多个直播间可能会触发反爬机制
经初步测试,监测一个直播间,每 3 分钟一次,很稳定

登录写评论