PHP simple_html_dom从url提取数据

PHP simple_html_dom从url提取数据,php,html,web-scraping,simple-html-dom,Php,Html,Web Scraping,Simple Html Dom,我的代码有什么问题?为什么它不起作用 这是我尝试使用的代码: function extract_data($url){ // Create DOM from URL $html = file_get_html($url); // initialize empty array to store the data array from each row $theData = array(); // loop over rows foreach($

我的代码有什么问题?为什么它不起作用

这是我尝试使用的代码:

function extract_data($url){

    // Create DOM from URL
    $html = file_get_html($url);

    // initialize empty array to store the data array from each row
    $theData = array();

    // loop over rows
    foreach($html->find('p.ch_title') as $row) {

        // initialize array to store the cell data from each row
        $rowData = array();
        foreach($row->find('p.ch_spec') as $cell) {

            // push the cell's text to the array
            $rowData[] = $cell->innertext;
        }

        // push the row's data array to the 'big' array
        $theData[] = $rowData;
    }

    return $theData;

}
这是来自url的html数据

<div class="holder-specificatii">
       <div class="box-specificatie">
          <div class="ch_group">Dimensiuni</div>
          <p class="ch_title">Latime (mm):</p>
          <p class="ch_spec">195</p>
          <p class="ch_title">Inaltime:</p>
          <p class="ch_spec">65</p>
          <p class="ch_title">Diametru (inch):</p>
          <p class="ch_spec">15</p>
          <div class="clear"></div>
       </div>
       <div class="box-specificatie">
          <div class="ch_group">Caracteristici tehnice</div>
          <p class="ch_title">Anotimp:</p>
          <p class="ch_spec">Iarna</p>
          <p class="ch_title">Indice sarcina:</p>
          <p class="ch_spec">91</p>
          <p class="ch_title">Indice viteza:</p>
          <p class="ch_spec">T</p>
          <p class="ch_title">Economie de carburant:</p>
          <p class="ch_spec">C</p>
          <p class="ch_title">Franare pe suprafete umede:</p>
          <p class="ch_spec">C</p>
          <p class="ch_title">Tip vehicul:</p>
          <p class="ch_spec">Turism</p>
          <p class="ch_title">DOT:</p>
          <p class="ch_spec">2014</p>
          <p class="ch_title">Nivel de zgomot (dB):</p>
          <p class="ch_spec">72dB</p>
          <div class="clear"></div>
       </div>
    </div>

迪曼苏尼
纬度(毫米):

195

时间:

65

直径(英寸):

15

特尼斯特征菌

另一个时间:

Iarna

指示八叠球菌:

91

指示viteza:

T

节能剂:

C

法国体育协会:

C

提示车辆:

旅游业

交通部:

2014年

尼维尔·德兹格莫特(dB):

72dB


问题在于返回空数组的函数。

如果指向未定义的对象,则应使用
$html

function extract_data($url){

    $html = file_get_html($url);
    $theData = array();
    // loop over rows
    foreach($html->find('div.box-specificatie') as $k => $row) { // loop each container
        $temp = array();
        // $main_title = $row->find('div.ch_group', 0)->innertext;
        foreach($row->find('p.ch_title') as $title) { // each title
            $spec = $title->next_sibling()->innertext(); // pair up with spec
            $temp[] = array('title' => $title->innertext, 'spec' => $spec);
        }
        $theData[$k] = $temp; // push inside
        // $theData[$main_title] = $temp; // optionally you can use a main title

    }

    return $theData;
}

echo '<pre>';
print_r(extract_data($url));
函数提取数据($url){
$html=file\u get\u html($url);
$theData=array();
//绕行循环
foreach($html->find('div.box-specifiatie')为$k=>$row){//循环每个容器
$temp=array();
//$main_title=$row->find('div.ch_group',0)->innertext;
foreach($row->find('p.ch_title')作为$title){//每个标题
$spec=$title->next_sibling()->innertext();//与spec配对
$temp[]=array('title'=>$title->innertext,'spec'=>$spec);
}
$theData[$k]=$temp;//推入
//$theData[$main\u title]=$temp;//您可以选择使用主标题
}
返回$theData;
}
回声';
打印(提取数据($url));

在第一个foreach中,你做得对,你使用的是从file\u get\u html收到的html,但在嵌套的foreach中,你使用的是返回的$行,它没有p.ch\u规范,因为它不是p.ch\u标题的子项。

你真的应该尝试缩小范围或更具体一些。你使用对象
$table
,在for循环中,哪个尚未定义?这应该会给你错误信息!是的,那是个小问题。将$table重新命名为$html,但它返回一个空数组。是否有方法为每个标题添加自定义字符串?@m3tsys自定义字符串是什么意思?既然您已经有了标题
$title->innertext
,您可以做任何需要做的事情do@m3tsys你是说这个元素<代码>尺寸?是的,我想你是对的!多谢各位<代码>$title->innertext大于enough@m3tsys好的,我很高兴这有帮助