NodeJS 如何在cheerio中获取背景图片url

jjjwad0x  于 2023-01-08  发布在  Node.js
关注(0)|答案(2)|浏览(160)

我试图得到我的动画星球帐户横幅我的刮刀系统。我尝试了一切我知道与cheerio但我不能得到profileBackgroundbackground-image网址。Properties
我试过了

async function Al() {
  const cheerio = require("cheerio");
  const url = "https://www.anime-planet.com/users/kyoyacchi";
  const {data} = await client.axios.get(url, {
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36",
    },
  });
  const $ = cheerio.load(data);

  return $(".pull-beta.pull-alpha.pullup.editableBanner")
    .find(".wrapper")
    .find("profileBackground");
}
Al();

Here is the result
这一条是只返回化身的路径。

m3eecexj

m3eecexj1#

我知道它是$('div[id=profileBackground]')

8ljdwjyq

8ljdwjyq2#

可以使用以下命令检索图像路径:

axios.get(url)
  .then(({data: html}) => {
    const $ = cheerio.load(html);
    const path = $("#profileBackground")
      .attr("style")
      .match(/background-image: *url *\((.+?)\)/)[1];
    console.log(path); // => /images/users/backgrounds/3966485.jpg?t=1660418964
  });

使用#some-id通过id属性进行选择。在CSS中,一个裸词指的是一个标记名,所以p<p></p>的选择器。id在文档中应该是唯一的,所以通常不需要指定父选择器。
上面的正则表达式从background-image: url后面的括号中提取内容。

相关问题