drwf69817 2018-06-12 17:25
浏览 44

如何通过PHP从特定HTML表中提取数据

I am wondering how do I get information from a specific table of an HTML document? Right now, I am pulling all of the data from the entire page, but I only want the data from the table with an id of team_batting. How would this be accomplished? Here is my code:

$dom = new DOMDocument();
$html = file_get_contents('https://www.baseball-reference.com/register/team.cgi?id=41270199');
@$dom->loadHTML($html);

$table = $dom->getElementByID('team_batting');

$stats = $dom->getElementsByTagName("td");

for ($i = 0; $i < $stats->length; $i++) {

    $attr = $stats->item($i)->getAttribute('data-stat');

    if ($attr != 'player') {
        continue;
    }

    $nodes[] = $stats->item($i)->textContent;

    foreach($nodes as $node) {
        echo $node;
        echo '<br>';
    }

}

With this I am able to pull all of the player names from the data-stat="player" attribute but I only want to apply this across the team_batting table. How would I do this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?