使用DOMDOCUMENT提取JavaScript值 [英] Using DOMDOCUMENT to pull JavaScript values
问题描述
是否可以使用DOM Document来提取javascript值?
< script type =text / javascript> ;
var datePickerDate =2014年10月14日;
$(document).ready(function(){
// meeting datepicker
$(#datepicker)。datepicker({
showOn:'button ',
buttonImage:'/images/calendar.gif',
buttonImageOnly:true,
onSelect:function(dateText,inst){
populate_meetings(dateText,0)
$,
dateFormat:'d MM yy',
minDate:new Date(2009,0,1),maxDate:0
});
$('#b $ b' datepicker('option','defaultDate',new Date(2014年10月14日));
$('#datepicker')。val(2014年10月14日);
});
//呼叫网页加载以获得会议和日期
populate_meetings(2014年10月14日,15515);
< / script>
这是在html中输出的,所以我知道它在那里...我想阅读日期所以拉var var datePickerDate =所以它返回2014年10月14日
- 稍微澄清一下---
即时通讯使用domdocument刮了一个网站,并抓住像这样的HTML
$ html = file_get_contents($ url);
//设置将被重用的主页面html代码
$ dom = new DOMDocument();
//载入html
@ $ dom-> loadHTML($ html);
在html中是一些javascript,它有一个日期....我想抓住这个日期,并坚持在一个可靠的
填充会议只是填充日期下拉框
你不能使用PHP执行javascript(显然),所以最好的办法是用正则表达式来获取JS变量。这里有一个如何使用DOMXPath的例子:
#创建一个新的XPath对象,在你的代码中初始化了
#
$ xp = new DOMXPath($ dom);
#查找页面中的所有脚本元素
$ scripts = $ xp-> query(// script);
foreach($ scripts作为$ s){
#查看脚本节点内容中是否有与var datePickerDate相匹配的内容
if(preg_match('#var datePickerDate =(。*? )#,$ s-> nodeValue,$ matches)){
#日期本身(在括号中被捕获)在$ matches [1]
print_r($ matches [1]);
$ / code $ / pre
$ b $输出:
2014年10月14日
Is it possible to use DOM Document to pull javascript values?
<script type="text/javascript">
var datePickerDate = "October 14, 2014";
$(document).ready(function(){
// meetings datepicker
$("#datepicker").datepicker({
showOn: 'button',
buttonImage: '/images/calendar.gif',
buttonImageOnly: true,
onSelect: function(dateText, inst) {
populate_meetings(dateText, 0)
},
dateFormat: 'd MM yy',
minDate: new Date(2009, 0, 1), maxDate: 0
});
$('#datepicker').datepicker('option', 'defaultDate', new Date("October 14, 2014"));
$('#datepicker').val("October 14, 2014");
});
//call on page load to get meetings and date
populate_meetings("October 14, 2014", 15515);
</script>
this is output in the html so i know its there... and i want to read the date so pull var datePickerDate = so it returns October 14, 2014
-- alittle more clarification---
im using domdocument to scrape a site and grab the html like so
$html = file_get_contents($url);
//set the main page html code that will be resused
$dom = new DOMDocument();
// load html
@$dom->loadHTML($html);
within the html is some javascript that has a date in it.... i would like to grab that date and stick it in a veriable
populate meetings just populates a dropdown box with the dates
解决方案 You can't execute the javascript using PHP (obviously), so the best thing might be to grab the JS variable with a regular expression. Here's an example of how to do it using DOMXPath:
# create a new XPath object that will operate on the DOM that you've initialised
# in your code
$xp = new DOMXPath($dom);
# find all the script elements in the page
$scripts = $xp->query("//script");
foreach ($scripts as $s) {
# see if there are any matches for var datePickerDate in the script node's contents
if (preg_match('#var datePickerDate = "(.*?)"#', $s->nodeValue, $matches)) {
# the date itself (captured in brackets) is in $matches[1]
print_r($matches[1]);
}
}
Output:
October 14, 2014
这篇关于使用DOMDOCUMENT提取JavaScript值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!