我正在使用php ganon dom解析器抓取一些html页面,但我被困在需要从我的javascript的源码中读取一些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();
}但这是行不通的。有关于如何阅读脚本的想法吗?
发布于 2013-04-28 15:30:09
尝试在$html对象的字符串中使用引号将type=text/javascript括起来。
我看了一下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中有从preg_match_all (Documentation)返回的URL数据。
如果它有帮助,一旦你有了网址,你可以在PHP中使用parse_url (Documentation),它会将字符串网址拆分成一些更容易处理的东西。
注意:使用的正则表达式是一个非常简单的表达式,不会涵盖所有情况。正如 和 所说,很难得到一个完美的正则表达式。
https://stackoverflow.com/questions/16260216
复制相似问题