2013-04-28 35 views
0

我正在使用php ganon dom解析器来抓取一些html页面,但我被卡在需要从源读取一些javascript的JavaScript。PHP ganon如何阅读javascript

<script type="text/javascript"> 
    Event.observe(window, 'load', function() { 
     ig_lightbox_main_img=0; 
ig_lightbox_img_sequence.push('http://someimageurl.com/image.jpg'); 
ig_lightbox_img_labels.push("Some text"); 
ig_lightbox_img_sequence.push('http://someimageurl.com/image2.jpg'); 
ig_lightbox_img_labels.push("Some text 2"); 
    }); 
</script> 

我想读的URL形成与我已经使用这个代码页的HTML未来对现在

$html = str_get_dom('some page html here'); 
    foreach($html('.product-img-box script[type=text/javascript]') as $script){ 
    echo $script->html(); 
} 

上面的脚本但是这是行不通的。关于如何阅读脚本的任何想法

回答

0

尝试将type=text/javascript左右的字符加到$html对象的字符串中。

我看看here,他们有一个例子:

foreach($html('a[href ^= "http://"]') as $element) { 
    $element->wrap('center'); 
} 

我认为这是/可能已经使其返回错误的结果。

编辑

被眼前的问题混为一谈,我认为问题是,你不能拿到剧本里面的数据,这是因为你的选择的。无论如何,经过一番思考,如果你有一个带有数据的脚本标签的字符串副本,只需在它上面运行一个正则表达式即可。

这里是我测试的一个示例:

$string = "<script type=\"text/javascript\"> 
    Event.observe(window, 'load', function() { 
     ig_lightbox_main_img=0; 
ig_lightbox_img_sequence.push('http://someimageurl.com/image.jpg'); 
ig_lightbox_img_labels.push(\"Some text\"); 
ig_lightbox_img_sequence.push('http://someimageurl.com/image2.jpg'); 
ig_lightbox_img_labels.push(\"Some text 2\"); 
    }); 
</script>"; 

$regex = "/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Za-z0-9+&@#\/%=~_|$?!:,.]*[A-Za-z0-9+&@#\/%=~_|$]/"; 

$results = array(); 

preg_match_all($regex,$string,$results); 

var_dump($results); 
//Result: array(1) { [0]=> array(2) { [0]=> string(33) "http://someimageurl.com/image.jpg" [1]=> string(34) "http://someimageurl.com/image2.jpg" } } 

$results具有在其内部的URL数据作​​为从preg_match_allDocumentation)返回。

如果有帮助,一旦你有了URL,你可以在PHP中使用parse_urlDocumentation),它将字符串URL分割成更容易使用的东西。

注意:使用的正则表达式是一个相当简单的表达式,并不会涵盖每种情况。如herehere所述,为此得到完美的正则表达式是非常困难的。

+0

我想读ig_lightbox_img_sequence.push('http://someimageurl.com/image2.jpg')http; $ html('a [href^=“http://”]')这个选择器是如何工作的...? – 2013-04-28 07:33:16

+0

不确定你的意思,你的选择器超出了我在'text/javascript'部分添加'''的提示,因此就像:'.product-img-box script [type =“text/javascript” ]',给这个镜头? – Turnerj 2013-04-28 07:35:15

+0

它只是没有找到任何东西来迭代? – Turnerj 2013-04-28 07:41:39