Retrieve data from the first td in every tr

2019-08-07 15:02发布

I'm scraping a page which contains of a table with several tr's. Inside every tr there's four td's, and I want to get the data from the first of these td's. Below is the code I've tried so far, but it grabs all the td's. How can I accomplish what I want?

...
$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    foreach($row->find('td', 0) as $cell) {
        echo $cell;
    }
}

2条回答
虎瘦雄心在
2楼-- · 2019-08-07 15:35

simple html dom is a turd. It's simpler to use the built in dom functions and xpath:

$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);

foreach($xpath->query('//td[1]') as $td){
  echo $td->nodeValue;
}

That said, I would probably still prefer to use phpquery

查看更多
狗以群分
3楼-- · 2019-08-07 15:45

Think about why you're using the second foreach when you actually only mean to act on one element within each row.

$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    $cell = $row->find('td', 0);
    echo $cell;
}
查看更多
登录 后发表回答