0

我一直在使用 X-Ray 来抓取运行良好的网站。我可以使用它很容易地引入图像。我遇到的一个问题是我没有看到一种简单的方法来抓取背景图像。假设我有一个 div,他们在该开发人员上设置样式属性,然后设置 URL,我不确定如何从中获取背景图像 url。我认为我不能只将特色图像属性传递给 css 属性,例如

 .featured-image.attr('background-image');

const getWebsiteContent = async (blogURL, selector) => {
  try {
    return await x(blogURL, selector, [{
      slug: 'a@href',
      featuredImage: 'img@src'
   }])
   .paginate(`${pagi}@href`)
   .limit(200)
   .then((response) => {
   spinner.succeed('Got the data');
   return response;
  })
} catch (error) {
  throw new Error('Cannot get Data from website, try checking your URL');
}
};
4

1 回答 1

0

对于任何想要使用 X 射线刮刀解决此问题的人,我最终所做的就是从您传递给对象的选择器中提取属性。鉴于 html 如下所示。

<div class="img" style="background-image: url('../path-to-img.jpg')"></div>

.img@src你可以写而不是写.img@style,这将返回给你样式属性。从那里您将需要使用正则表达式来删除不是图像 URL 的其余不需要的数据。

于 2019-05-06T15:09:58.940 回答