简体   繁体   English

没有属性的简单Dom HTML标记

[英]Simple Dom HTML tags without attributes

Hello I am trying to pull back roster information from ESPN.com. 您好,我正在尝试从ESPN.com提取花名册信息。 Each team's roster is saved into a table. 每个团队的花名册都保存在表格中。 I am trying to figure a way to save each tag into a variable as appropriate however each tag does not have an ID such as "jersey_number"/"player_name" so search through this has given me some problems. 我正在尝试找到一种将每个标签适当地保存到变量中的方法,但是每个标签都没有诸如“ jersey_number” /“ player_name”之类的ID,因此对此进行搜索给了我一些问题。 Here is what I have so far - If you could give me a pointer or 2 that would be much appreciated. 这是我到目前为止的内容-如果您能给我一个或两个指针,将不胜感激。

    <?php
    require_once("../tools/simple_html_dom.php");
    require_once("../tools/Utilities.php");
    $url = "http://espn.go.com/nfl/team/roster/_/name/den/denver-broncos";

    $espnHTML = file_get_html("http://espn.go.com/nfl/team/roster/_/name/den/denver-broncos");



    foreach($espnHTML->find("table.tablehead",0)->find('tr[class^=odd]') as $rosterRow)
    {
        foreach($rosterRow->find("td") as $playerInfo)
        {
            echo $playerInfo->plaintext."<br>";   
        }

    }
   ?>

How can I assign these td tags into appropriate variables without "ids"? 如何将这些td标签分配给没有“ id”的适当变量? Attached is a sample screenshot that may help you understand what I am talking about. 随附的示例屏幕截图可以帮助您了解我在说什么。 在此处输入图片说明

If the columns are in the same order for every player, using your $rosterrow->find("td") should return an indexed array that you can access using $playerrow[0..n] . 如果每个玩家的列顺序都相同,则使用$rosterrow->find("td")应该返回一个索引数组,您可以使用$playerrow[0..n]进行访问。 Then, by analyzing what corresponds to what you can make a function like this: 然后,通过分析与之对应的内容,您可以创建如下函数:

$players = array();
foreach($espnHTML->find("table.tablehead",0)->find('tr[class^=odd]') as $rosterRow)
{
    $playerRow = $rosterRow->find("td");
    $name = $playerRow[0];
    $jersey = $playerRow[1];
    // more can be added, of course.

    $players[$name] = array();
    $players[$name]["jersey"] = $jersey;
    // and others
}

For table 对于表

John Appleseed | 12
---------------|----
Richard Brooks | 34

this will result in an array like 这将导致像

{ "John Appleseed" => { "jersey" => 12 }, "Richard Brooks" => { "jersey" => 34}}

Please let me know if this helped. 请告诉我是否有帮助。

If you're open to a different approach that may be more scalable/robust, then you may also want to take a look at Kimono Labs . 如果您愿意采用一种可能更具可扩展性/可靠性的方法,那么您可能还想看看Kimono Labs You can use it to create structured API based on ESPN's data. 您可以使用它基于ESPN的数据创建结构化的API。 I think you'd be able to define which part of the table held names, scores, etc. and would easily be able to call the API for the desired info. 我认为您将能够定义表的哪一部分包含名称,分数等,并且可以轻松地为所需信息调用API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM