我有一些动态生成iframe元素的代码(它是显示Google广告代码时生成的代码)。在生成的HTML中是一个img元素,其中包含我需要检索的图像URL。如何搜索生成的HTML以检索img url?对于凌乱的格式,很抱歉(div id =“div-gpt-ad-1452138910394-0”)包含生成的HTML以及我需要获取的img url。
<html><head>
<meta charset="utf-8">
<title>Truck</title>
<meta name="generator" content="Google Web Designer 1.5.3.1209">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css" id="gwd-text-style">
p {
margin: 0px;
}
h1 {
margin: 0px;
}
h2 {
margin: 0px;
}
h3 {
margin: 0px;
}
</style>
<style type="text/css">
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
body {
transform: perspective(1400px) matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
transform-style: preserve-3d;
background-color: transparent;
}
</style>
<script type="text/javascript">
(function() {
var useSSL = 'https:' == document.location.protocol;
var src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
})();
</script><script src="http://www.googletagservices.com/tag/js/gpt.js"></script><script id="gpt-impl-0.6896143630588139" src="http://partner.googleadservices.com/gpt/pubads_impl_78.js"></script><iframe style="visibility: hidden; display: none;" src="http://tpc.googlesyndication.com/safeframe/1-0-2/html/container.html"></iframe>
<script type="text/javascript">
googletag.cmd.push(function() {
googletag.defineSlot('/3576361/Truck1', [300, 600], 'div-gpt-ad-1452138910394-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().enableSyncRendering();
googletag.enableServices();
});
</script>
<script src="http://pagead2.googlesyndication.com/pagead/osd.js" type="text/javascript"></script></head><iframe src="" style="display: none;"></iframe>
<body>
<!-- /3576361/Truck1 -->
<div id="div-gpt-ad-1452138910394-0" style="height:600px; width:300px;">
<script type="text/javascript">
googletag.cmd.push(function() {
googletag.display('div-gpt-ad-1452138910394-0');
});
</script><script type="text/javascript" src="https://securepubads.g.doubleclick.net/gampad/ads?gdfp_req=1&correlator=1156306692399781&output=json_html&callback=googletag.impl.pubads.setAdContentsBySlotForSync&impl=ss&json_a=1&eid=108809080&sc=0&sfv=1-0-2&iu_parts=3576361%2CTruck1&enc_prev_ius=%2F0%2F1&prev_iu_szs=300x600&cookie_enabled=1&lmt=1452538211&dt=1452541941283&cc=100&frm=20&biw=1161&bih=742&oid=3&adks=2284442022&gut=v2&ifi=1&u_tz=-480&u_his=2&u_java=true&u_h=900&u_w=1440&u_ah=829&u_aw=1440&u_cd=24&u_nplug=3&u_nmime=23&u_sd=1&flash=0&url=file%3A%2F%2F%2FUsers%2Fcollinschupman%2FDesktop%2Fadtest.html&dssz=6&icsg=8&std=3&csl=134&vrg=78&vrp=78&ga_vid=1010714188.1452541941&ga_sid=1452541941&ga_hid=1562121578"></script><div id="div-gpt-ad-1452138910394-0_ad_container">
<ins style="width: 300px; height: 600px; display: inline-table; position: relative; border: 0px none; vertical-align: bottom;"><ins style="width: 300px; height: 600px; display: block; position: relative; border: 0px none;"><iframe style="border: 0px none; position: absolute; top: 0px; left: 0px;" marginheight="0" marginwidth="0" scrolling="no" name="google_ads_iframe_/3576361/Truck1_0" id="google_ads_iframe_/3576361/Truck1_0" frameborder="0" height="600" width="300"></iframe></ins></ins></div>
<script>googletag.impl.pubads.createDomIframe("div-gpt-ad-1452138910394-0_ad_container" ,"/3576361/Truck1_0",false,false);</script>
</div>
<!-- Click Test -->
<a id="something">Text</a>
<script type="text/javascript">
$('#something').click( function()
{
alert('clicked');
var someimage = document.getElementById('div-gpt-ad-1452138910394-0');
console.log(someimage)
// var myimg = someimage.getElementsByTagName('img')[0];
// console.log(myimg)
// var mysrc = myimg.src;
// console.log(mysrc)
});
</script>
<iframe style="display: none; width: 0px; height: 0px;" name="google_osd_static_frame" id="google_osd_static_frame_9257546990485"></iframe></body></html>
答案 0 :(得分:0)
这有点&#34; hacky&#34;,但您可以使用此代码获取自定义类型广告素材的代码
googletag.pubads().addEventListener('slotRenderEnded', function (event) {
var containerId = event.slot.getSlotElementId();
var containerEl = document.getElementById(containerId);
if (containerEl === null) return;
var iframeEl = containerEl.querySelectorAll('iframe')[0];
console.log(iframeEl.name);
});
你的html是用你将获得的字符串编码的。