从外部网站获取 DIV 内容

2022-08-30 18:42:53

我想用纯PHP从外部网站获取DIV。

外部网站:http://www.isitdownrightnow.com/youtube.com.html

Div 文本 I want from isitdownrightnow (statusup div):<div class="statusup">The website is probably down just for you...</div>

我已经尝试过和,但我无法让它工作。file_get_contentsDOMDocumentstr_get_html

例如,这个

$page = file_get_contents('http://css-tricks.com/forums/topic/jquery-selector-div-variable/');
    $doc = new DOMDocument();
    $doc->loadHTML($page);
    $divs = $doc->getElementsByTagName('div');
    foreach($divs as $div) {
        // Loop through the DIVs looking for one withan id of "content"
        // Then echo out its contents (pardon the pun)
        if ($div->getAttribute('class') === 'bbp-template-notice') {
             echo $div->nodeValue;
        }
    }

它只会在控制台中显示一个错误:

无法加载资源:服务器以 500 状态响应(内部服务器错误)


答案 1

这就是我一直使用的:

$url = 'https://somedomain.com/somesite/';
$content = file_get_contents($url);
$first_step = explode( '<div id="thediv">' , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];

答案 2

这可能有点过分,但你会明白要点。

<?php 

$doc = new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;

// Most HTML Developers are chimps and produce invalid markup...
$doc->strictErrorChecking = false;
$doc->recover = true;

$doc->loadHTMLFile('http://www.isitdownrightnow.com/check.php?domain=youtube.com');

$xpath = new DOMXPath($doc);

$query = "//div[@class='statusup']";

$entries = $xpath->query($query);
var_dump($entries->item(0)->textContent);

?>

推荐