我想制作一个新闻网站,就像我在 Internet 上出现的其他网站一样。
我没有足够的内容。所以我想从另一个新闻网站获取内容(当然,我得到了从他们网站复制他们内容的许可)。
为此我使用了 Simple HTML DOM ,它在我的其他网站上进展顺利,但当我在发布网站上应用我的脚本时,它不起作用(不输出任何内容)。
这是我想从中获取数据的链接:hesspress.com .
代码:
require_once 'simple_html_dom.php';
$lien='http://www.hesspress.com';
$html=new simple_html_dom();
$html->load_file($lien);
$k=$html->find('a',0)->href;
print_r($k);
最佳答案
这就是我要使用的,您可以根据自己的目的修改它。
它使用 built-in PHP DOM .
代码:
<?php
$url = 'http://acid3.acidtests.org';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($handle);
libxml_use_internal_errors(true); // Prevent HTML errors from displaying
$doc = new DOMDocument();
$doc->loadHTML($html);
$link = $doc->getElementsByTagName('a')->item(0);
echo 'Link text: ' . $link->nodeValue;
echo '<br />';
echo 'Link URL: ' . $link->getAttribute('href');
?>
关于php - 如何使用 PHP 解析网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028972/