【问题标题】:Extract Data from HTML using PHP使用 PHP 从 HTML 中提取数据
【发布时间】:2011-04-08 16:14:40
【问题描述】:

这就是我要找的东西:

我有一个显示 HTML 格式数据的链接:

http://www.118.com/people-search.mvc...0&pageNumber=1

数据格式如下:

<div class="searchResult regular"> 

鸟约翰

利思韦特路 56 号
伦敦
SW11 6RS 020 7228 5576

我希望我的 PHP 页面执行上面的 URL 并根据上面的标签从结果 HTML 页面中提取/解析数据 h2=名称 地址=地址 phoneNumber= 电话号码

并以表格格式显示它们。

我知道了,但它只显示 HTML 页面的 TEXT 格式,但在一定程度上可以:

<?
function get_content($url) 
{ 
$ch = curl_init(); 

curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_HEADER, 0); 

ob_start(); 

curl_exec ($ch); 
curl_close ($ch); 
$string = ob_get_contents(); 

ob_end_clean(); 

return $string; 

} 


$content = get_content("http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=Crabtree&pageSize=50&pageNumber=1"); 
echo $content;
$content = get_content("http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=Crabtree&pageSize=50&pageNumber=2"); 
echo $content;
$content = get_content("http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=Crabtree&pageSize=50&pageNumber=3"); 
echo $content;
$content = get_content("http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=Crabtree&pageSize=50&pageNumber=4"); 
echo $content;

?>

【问题讨论】:

标签: php html extract html-content-extraction


【解决方案1】:

你需要使用一个dom解析器Simple HTML或者类似的

将文件读入一个 dom 对象并使用适当的选择器对其进行解析:

$html = new simple_html_dom("http://www.118.com/people-search.mvc...0&pageNumber=1");

foreach($html->find(.searchResult+regular) as $div) {
  //parse div contents here to extract name and address etc.
}
$html->clear();
unset($html);

有关详细信息,请参阅Simple HTML 文档。

【讨论】:

    猜你喜欢
    • 2013-04-05
    • 2016-09-10
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2015-08-22
    • 2015-06-09
    • 2013-06-12
    相关资源
    最近更新 更多