只有在源页面上执行了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个回答



cURL不会执行任何javascript,同样适用于 file_get_contents </ code>。 您将只获得没有动态呈现内容的DOM。</ p>

您应该尝试探索如何将该网站上的图形加载到页面中。 您可以使用chrome或firefox [F12]的webdeveloper工具,并在加载应用程序时检查网络选项卡以查找资源URL。</ p>

如果您很幸运,您可能能够 直接从该源获取数据。 希望这有帮助!</ p>
</ div>

展开原文

原文

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!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐