Php 使用curl和regex获取表数据

Php 使用curl和regex获取表数据,php,html,xpath,web-scraping,domdocument,Php,Html,Xpath,Web Scraping,Domdocument,这是我从表中提取数据的代码 但我想删除链接 以及工件的名称和价格如何排列 <?php $ch = curl_init ("http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($ch); preg_match('#<table[^>]*>(.+?)</t

这是我从表中提取数据的代码

但我想删除链接

以及工件的名称和价格如何排列

<?php

$ch = curl_init ("http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

preg_match('#<table[^>]*>(.+?)</table>#is', $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';

echo  $match ;
echo '</table>';

?>  

如果要解析某些web资源,可以使用

如果要获取表和表内的所有链接,请执行以下操作:

$html = file_get_html('http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301');
$table = $html->find('table');
$links = $table->find('a');

echo $table;

我建议改用HTML解析器。使用
DOMDocument
+
DOMXpath
,无需安装,它们已内置。例如:

$ch = curl_init ("http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$data = array();
// get all table rows and rows which are not headers
$table_rows = $xpath->query('//table[@id="tbl-all-product-view"]/tr[@class!="rowH"]');
foreach($table_rows as $row => $tr) {
    foreach($tr->childNodes as $td) {
        $data[$row][] = preg_replace('~[\r\n]+~', '', trim($td->nodeValue));
    }
    $data[$row] = array_values(array_filter($data[$row]));
}

echo '<pre>';
print_r($data);

最终输出是什么?提取产品名称和价格//语言是波斯语并将所有数据放入数组中?使用top过程无法提取到数组中//我想将数据提取到array@amirrasabeh当然没问题,很高兴它能帮你每天用curl更新页面。当然,我想whit corn的工作就是更新页面。。但我不知道如何使用它,请看这个问题@Ghost谢谢你的代码,但你能帮我处理表的其他结构吗?这是我需要的:。Regards@Peacefull想法是一样的,使用curl获取html,使用
DOM
解析实际的html
Array
(
    [0] => Array
    (
        [0] => AMDA4-3400
        [1] => 1,200,000
        [2] => 1,200,000
    )

    [1] => Array
    (
        [0] => AMDSempron 145
        [1] => 860,000
        [2] => 910,000
    )