Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Cheerio获取style=背景img URL_Javascript_Html_Jquery_Node.js_Cheerio - Fatal编程技术网

Javascript 如何使用Cheerio获取style=背景img URL

Javascript 如何使用Cheerio获取style=背景img URL,javascript,html,jquery,node.js,cheerio,Javascript,Html,Jquery,Node.js,Cheerio,我正在尝试获取背景图像的URL。背景图像位于a href标记中 a href标签,style=“后台img:url(“”) 我使用的是cheerio(node.js模块类似于Jquery)。当我试着 这给了我一个错误 类型错误:无法读取未定义的属性“替换” 我的代码是: $("div.listing.listing-blog.listing-blog-1.clearfix.columns-1.columns-1 > article > .item-inner .featu

我正在尝试获取背景图像的URL。背景图像位于a href标记中

a href标签,
style=“后台img:url(“”)

我使用的是cheerio(node.js模块类似于Jquery)。当我试着 这给了我一个错误

类型错误:无法读取未定义的属性“替换”

我的代码是:

$("div.listing.listing-blog.listing-blog-1.clearfix.columns-1.columns-1 > article > .item-inner  .featured.clearfix  a").map(function () {
    let imgURL = $(this).css("background-image");
    imgURL = imgURL.replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, '');
    return { imgURL };
  }).get();
这是需要背景图像URL的元素:

<article class="post-431236 type-post format-standard has-post-thumbnail   listing-item listing-item-blog  listing-item-blog-1 main-term-51 bsw-5 ">
<div class="item-inner clearfix">
<h2 class="title"> <a href="https://arynews.tv/en/nab-money-laundering-reference-shehbaz-sharif/" class="post-url post-title">
POST TITLE </a>
</h2>
<div class="featured clearfix">
<a alt="nab, shehbaz sharif, pml-n, benami assets" title="NAB prepares money-laundering reference against Shehbaz Sharif: sources" data-bs-srcset="{&quot;baseurl&quot;:&quot;https:\/\/arynews.tv\/wp-content\/uploads\/2020\/09\/&quot;,&quot;sizes&quot;:{&quot;210&quot;:&quot;shehbaz-1-1-210x136.jpg&quot;,&quot;279&quot;:&quot;shehbaz-1-1-279x220.jpg&quot;,&quot;357&quot;:&quot;shehbaz-1-1-357x210.jpg&quot;,&quot;750&quot;:&quot;shehbaz-1-1.jpg&quot;}}" class="img-holder    b-loaded" href="https://arynews.tv/en/nab-money-laundering-reference-shehbaz-sharif/" style="background-image: url(&quot;https://arynews.tv/wp-content/uploads/2020/09/shehbaz-1-1-279x220.jpg&quot;);"></a>
</div>

使用纯
js
而不使用
jQuery
您可以键入:

const selector = `div.listing.listing-blog.listing-blog-1.clearfix.columns-1.columns-1 > article > .item-inner  .featured.clearfix  a`;

const imgURL = document.querySelector(selector).style.background
您将收到类似字符串的

"url("http://localhost:1337/uploads/offline_reset_45146034ad.jpg") center center no-repeat"
下一步是通过正则表达式从这个字符串获取URL

你的例子是:

imgURL.replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, '')
给别人很大的帮助

"http://localhost:1337/uploads/offline_reset_45146034ad.jpg"

cheerio没有这些方法,您需要使用attr():


jQuery没有问题,所以您确定选择器是正确的吗?您能为我们展示元素的CSS吗?无法读取未定义的属性-只需添加一个子句,类似于
let imgURL=$(this).CSS(“背景图像”)| |“”我使用的是Cheerio,它类似于Jquery。但是我没有定义。它返回给我imgURL='',我该怎么办?显示
document.querySelector(selector).innerHTML
,并将其粘贴到您的问题中我已添加了元素,在问题的上面。检查一下,我想得到img的URL。这是时尚背景,非常感谢!我试过了,但没有成功。尽管如此,我已经添加了元素,请检查上面的内容。我需要背景图片的网址,这是在风格。谢谢。为我工作
// untested, I have no idea what your element looks like
$(a).attr('style').match(/url\("(.*?)"/)[1]