dshp9580656 2018-12-26 00:40
浏览 70

只有在源页面上执行了javascript之后才能回显span的内容的PHP方法

Hi there I am looking for a simple PHP only method (i.e. no JS/Jquery/Ajax on the end user) method of doing a file_get_contents or cURL after the JS has run on the source page.

I have 2 working examples, getting the specific SPAN with Class="CurrentBG" from the remote, however the content of this SPAN is created by a JS file which runs right at the end of the page. As such using both cURL and get_file_contents is leaving me with the --- as the content and printing that, rather than the varying content (this is the default content of this SPAN until it's replaced by the JS function).

Here is my get_file_contents example

<?php 

$page = file_get_contents('https://haspdenbloodglucose.herokuapp.com/');
$doc = new DOMDocument();
$doc->loadHTML($page);
$spans = $doc->getElementsByTagName('span');
foreach($spans as $span) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($span->getAttribute('class') === 'currentBG') {
         echo $span->nodeValue;
    }
}

?>

Here is my cURL example

<?php
$curl = curl_init('https://haspdenbloodglucose.herokuapp.com/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

curl_close($curl);

$regex = '/<span class="currentBG">(.*?)<\/div>/s';
if ( preg_match($regex, $page, $list) )
    echo $list[0];
else 
    print "Not found"; 
?>

I want to run this from a browser on a browser on Amazon Kindle Paperwhite / touch (don't ask me why haha). however sites are very limited in functionality so I an trying to keep what runs on the end users kindle as limited as possible...

This will be used to show the users blood glucose data on their tablets in a simple digital clock face style display.

Thanks

  • 写回答

1条回答 默认 最新

  • doucong8553 2018-12-26 01:28
    关注

    cURL will not execute any javascript, the same applies to file_get_contents. You will only get the DOM with no dynamically rendered content.

    You should try to explore how the graph on that website is being loaded into the page. You could use the webdeveloper tool of chrome or firefox [F12] and check the networking tab while the app is loading to find out the resource url.

    If you're lucky you might be able to get the data directly from that source. Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler