从javascript的内容获取所有图像链接

时间:2018-04-08 08:42:29

标签: javascript php json simple-html-dom

 <script type="text/javascript">
    var productJsonMedias = 
    [
        {"thumb":"domain.com/a-1.jpg","zoom":"domain.com/b-1.jpg","description":"","altText":"Product Name"},   
        {"thumb":"domain.com/a-2.jpg","zoom":"domain.com/b-2.jpg","description":"","altText":"Product Name"}    
    ];

我想从json的内容中获取所有图像链接,这个scipt嵌入到web上。我正在使用Simple Html Dom来抓取网页。

2 个答案:

答案 0 :(得分:0)

如果你想从JSON获得,你应该使用AJAX:

&#13;
&#13;
var data = [];
var xhr = new XMLHttpRequest();

  xhr.open('GET', '*LINK TO JSON*', true);
    xhr.send();
    xhr.onreadystatechange = function() {
     if (xhr.readyState != 4) return;
     if (xhr.status != 200) {
      alert(xhr.status + ': ' + xhr.statusText);
     } else {
      try{         
        data = JSON.parse(xhr.responseText);
      }catch(e){
        alert("ERROR: " + e);
      }
    }
   }
&#13;
&#13;
&#13;

如果你想从页面获取HTML代码,你应该使用php或node.js(你应该发送GET请求并解析代码)。

最好发送JSON数据的ajax GET请求。

答案 1 :(得分:0)

你需要使用正则表达式来获取json:

$str = <<<EOF
<script type="text/javascript">
    var productJsonMedias = 
    [
        {"thumb":"domain.com/a-1.jpg","zoom":"domain.com/b-1.jpg","description":"","altText":"Product Name"},   
        {"thumb":"domain.com/a-2.jpg","zoom":"domain.com/b-2.jpg","description":"","altText":"Product Name"}    
    ];
</script>
EOF;

$html = str_get_html($str);
$script = $html->find('script', 0);
if(!preg_match('/\[.*\]/', $script, $m)) die('cant find json!');

print_r(json_decode($m[0], true));

/*
(
    [0] => Array
        (
            [thumb] => domain.com/a-1.jpg
            [zoom] => domain.com/b-1.jpg
            [description] => 
            [altText] => Product Name
        )

    [1] => Array
        (
            [thumb] => domain.com/a-2.jpg
            [zoom] => domain.com/b-2.jpg
            [description] => 
            [altText] => Product Name
        )

)
*/