【问题标题】:Getting values from webpage using Simple HTML Dom library使用 Simple HTML Dom 库从网页中获取值
【发布时间】:2013-10-24 00:53:58
【问题描述】:

我正在尝试从网页中的表格中获取值,为此我正在使用简单的 HTML Dom 库。这就是我的代码的样子:

include('simple_html_dom.php');

$html = file_get_html('http://www.lvbp.com/posicion.html');

$arr = array();
foreach ($html->find('tr') as $e) {
    array_push($arr, $e->innertext);
}

echo '<pre>';
print_r($arr);
echo '</pre>';

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

print_r($arr):

Array
(
    [0] =>       EQUIPOS      J      G      P      Vent    
    [1] => 
    [2] =>       Navegantes      11      8      3      0    
    [3] =>       Tigres      11      8      3      0    
    [4] =>       Caribes      11      6      5      2    
    [5] =>       Leones      11      6      5      2    
    [6] =>       Aguilas      11      5      6      3    
    [7] =>       Tiburones      10      4      6      3.5    
    [8] =>       Cardenales      10      3      7      4.5    
    [9] =>       Bravos      11      3      8      5    
)

但是从这里我需要分别表示“Navegantes”、“11”、“8”等等......对于每个数组位置。为此我的最后一个代码:

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

但它不起作用,因为我得到了这个结果:

Navegantes 11 8 3 0 Tigres 11 8 3 0 Caribes 11 6 5 2 Leones 11 6 5 2 Aguilas 11 5 6 3 Tiburones 10 4 6 3.5 Cardenales 10 3 7 4.5 Bravos 11 3 8 5 

我错过了什么?有什么帮助吗?

更新

根据建议,我的代码如下所示:

include('simple_html_dom.php');
$html = file_get_html('http://www.lvbp.com/posicion.html');

$arr = array();
foreach ($html->find('tr') as $e) {
    $narr = array();
    foreach ($e->find('td') as $vp) {
        array_push($narr, $vp->plaintext);
    }
    $arr[] = array($narr);
}

【问题讨论】:

  • 使用 $arr[$i]=str_replace("", "-", $arr[$i]);而不是 str_replace("", "-", $arr[$i]);
  • @Subin 更改但得到相同的结果,单词之间没有破折号 (-)
  • 您是否尝试将 tr 中的每个 td 值作为数组获取?
  • @Subin,是的,这正是我想要做的

标签: php php-5.3 simple-html-dom


【解决方案1】:

试试这个:

$arr = array();
foreach ($html->find('tr') as $e) {
 $narr=array();
 foreach($e->find('td') as $vp){
  array_push($narr,$vp->plaintext);
 }
 $arr[]=array($narr);
}

而不是:

foreach ($html->find('tr') as $e) {
    array_push($arr, $e->innertext);
}

并删除代码:

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

您将获得一个数组,其中键为 tr 标记,其值为 tr 的每个 td

【讨论】:

  • 不,你错了我得到这个错误:PHP Fatal error: Call to a member function find() on a non-object in /var/www/html/reader/parser/posicion.php on line 29, referer: http://devserver/reader/parser/ where line 29 is foreach($code-&gt;find('td') as $vp){ ...
  • 你确定你换对了吗?因为在我这边它正在工作。
  • 您使用的是最新版本的 simple_html_dom 吗? simplehtmldom.sourceforge.net
  • 是的,我几秒钟前下载了
【解决方案2】:

这是一个方法:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = "http://www.lvbp.com/posicion.html";

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load_file($url);

// parse rows
foreach ($html->find('tr') as $i => $row) {

    // Skip the second empty row
    if ($i == 1)
        continue;

    // parse and print cells
    foreach ($row->find('td') as $j => $col) {
        echo $col->plaintext;
        echo "|";
    }
    echo "<hr>";
}


// Clear DOM object (needed essentially when using many)
$html->clear(); 
unset($html);

Live DEMO

【讨论】:

    猜你喜欢
    • 2013-07-22
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多