从每个 tr 的第一个 td 检索数据 [英] Retrieve data from the first td in every tr

查看:43
本文介绍了从每个 tr 的第一个 td 检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取一个页面,其中包含一个带有多个 tr 的表格.在每个 tr 中有四个 td,我想从这些 td 中的第一个获取数据.下面是我到目前为止尝试过的代码,但它抓住了所有的 td.我怎样才能完成我想要的?

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;
    }
}

推荐答案

想想为什么要使用第二个 foreach,而实际上你只想对每个 行中的一个元素进行操作.

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;
}

这篇关于从每个 tr 的第一个 td 检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆