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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。