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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿