我正在开发一个应用程序,我需要从另一个来源/网站获取数据。我做了谷歌并测试了一些选项,如 file_get_contents / Curl。很明显 curl 比 file_get_contents 快得多。
现在,我需要获取的是某个内容列表,例如 div 中的项目列表。我想忽略 HEAD 信息/脚本/css 等。只需要获取数据并在我自己的主题中设置样式。
请告诉我获取内容和样式输出的最佳方法/功能。如果您可以抽出时间编写基本代码,那将非常有帮助...
提前致谢
我会使用 HTML 解析器。我使用简单的 HTML DOM 解析器:http://simplehtmldom.sourceforge.net/ ,它很棒。还有一个很棒的文档。它在其主页中给出了这个例子:
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
你可以做到这一点的一种方法file_get_contents
是使用.
例子:
$fileHTML = file_get_contents('http://www.google.ca');
$title = substr($fileHTML,strpos($fileHTML,'<title>') + 7,strpos($fileHTML,'</title>') - (strpos($fileHTML,'<title>') + 7));
+ 7 所以它将转到 <title> 的末尾
echo $title;
这应该显示 Google 网站的标题。
可行的方法是获取 ID,因为在适当的网站中,具有 id 的所有内容都是唯一的。