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 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名