duanjianlu0506 2014-10-17 11:22
浏览 47
已采纳

使用DOMDOCUMENT来提取JavaScript值

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

  • 写回答

1条回答 默认 最新

  • doulu8341 2014-10-17 11:58
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记