Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.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 Phantomjs获取链接href及其子对象_Javascript_Phantomjs - Fatal编程技术网

Javascript Phantomjs获取链接href及其子对象

Javascript Phantomjs获取链接href及其子对象,javascript,phantomjs,Javascript,Phantomjs,我正试图从一个带有phantomjs的网页上获取链接href及其img src的元素列表 网页示例: <ul> <li> <a href="link A"> <img src="link B"></img> </a> </li> </ul> <html> <body> <ul>

我正试图从一个带有phantomjs的网页上获取链接href及其img src的元素列表

网页示例:

  <ul>
    <li>
      <a href="link A">
        <img src="link B"></img>
      </a>
   </li>
 </ul>
<html>
    <body>
        <ul>
            <li>
              <a href="http://google.com">
                <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"></img>
              </a>
           </li>
         </ul>    
    </body>
</html>
我试过:

 var lis = page.evaluate(function() {
         var link_img = new Array;
         SrcAlt = [];
         var li = document.getElementsByTagName("li");

         for(q = 0; q < li.length; q++){

           var a = li[q].getElementsByTagName('a')[0];


            var link = a.getElementsByTagName("img")[0];


            SrcAlt.push({"img": a.href, "link": link.src});

    }
    return SrcAlt;
});
var lis=page.evaluate(函数(){
var link_img=新数组;
sralt=[];
var li=document.getElementsByTagName(“li”);
对于(q=0;q
它只是返回“img”:a.href正确,我无法获得“链接”,我不知道为什么。我怎样才能修好它


谢谢

您应该提取图像的
src
属性,就像您对
a.href
所做的那样:

SrcAlt.push({"img": a.href, "link": link.src});
更新

为了确认答案的有效性,我对一个测试网页运行了一个测试脚本

网页:

  <ul>
    <li>
      <a href="link A">
        <img src="link B"></img>
      </a>
   </li>
 </ul>
<html>
    <body>
        <ul>
            <li>
              <a href="http://google.com">
                <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"></img>
              </a>
           </li>
         </ul>    
    </body>
</html>

如果答案仍然不适用于您,请向我们展示您的脚本、您的实际网页代码、脚本运行的结果、PhantomJS的版本-我们将尝试找出问题所在。

您应该提取图像的
src
属性,就像您对
a.href
所做的那样:

SrcAlt.push({"img": a.href, "link": link.src});
更新

为了确认答案的有效性,我对一个测试网页运行了一个测试脚本

网页:

  <ul>
    <li>
      <a href="link A">
        <img src="link B"></img>
      </a>
   </li>
 </ul>
<html>
    <body>
        <ul>
            <li>
              <a href="http://google.com">
                <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"></img>
              </a>
           </li>
         </ul>    
    </body>
</html>
如果答案仍然不适用于您,请向我们展示您的脚本、您的实际网页代码、脚本运行的结果、PhantomJS的版本-我们将尝试找出问题所在