正在阅读DOM PHP的部分内容


Reading parts of DOM PHP

我试图从网站上读取某些部分,但我无法正确显示它,它没有抓住正确的部分,而是只显示所有部分(整个页面),而不是我选择的对象。

这是我试图复制的页面。http://www.jabbithole.com/classes/engineer

我想要的是图像、拼写名称和类型作为3个变量,这样我就可以将它们放入一个名为"spells"的数组中。

$items = array('engineer');
foreach($items as $key => $value) {
            $url = 'http://www.jabbithole.com/classes/'.$value;     
            $html = file_get_contents($url);            
            $dom = new domDocument;
            @$dom->loadHTML($html);         
            $dom->preserveWhiteSpace = false;   
            $info = $dom->getElementsByTagName('tbody');    
            $trs = $dom->getElementsByTagName('tr');
            $spells = array();
            foreach($trs as $tr) {  
                foreach($tr->getElementsByTagName('td') as $row) {  
                    foreach($row->getElementsByTagName('a') as $link) { 
                        $class = $link->getAttribute('class');  
                        if($class == 'icon')
                        {
                            $img = $link->getAttribute('src');                      
                            print_r($img);
                        } 
                        else 
                        {
                            $name = $link->nodeValue;                   
                            print_r($name);
                        }
                    }
                }           
            }
}

标记我正试图让它读

<table class="datalist dataTable">
  <thead>
    <tr>
      <th class="left">Name</th>
      <th>Type</th>
      <th class="left int-sort">Source</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/mode-eradicate-47866">
          <img alt="Icon skillengineer eradication mode.36" src="/assets/icons/icon_skillengineer_eradication_mode.36.png" />
        </a> <a href="/spells/mode-eradicate-47866">Mode: Eradicate</a>
      </td>
      <td class="center">Innate</td>
      <td>Class innate</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/mode-provoke-47881">
          <img alt="Icon skillengineer survival mode.36" src="/assets/icons/icon_skillengineer_survival_mode.36.png" />
        </a> <a href="/spells/mode-provoke-47881">Mode: Provoke</a>
      </td>
      <td class="center">Innate</td>
      <td>Class innate</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/pulse-blast-42276">
          <img alt="Icon skillengineer pulse blast.36" src="/assets/icons/icon_skillengineer_pulse_blast.36.png" />
        </a> <a href="/spells/pulse-blast-42276">Pulse Blast</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 1</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/electrocute-41276">
          <img alt="Icon skillengineer electrocute.36" src="/assets/icons/icon_skillengineer_electrocute.36.png" />
        </a> <a href="/spells/electrocute-41276">Electrocute</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 2</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/artillery-bot-42814">
          <img alt="Icon skillengineer artillery bot.36" src="/assets/icons/icon_skillengineer_artillery_bot.36.png" />
        </a> <a href="/spells/artillery-bot-42814">Artillerybot</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 6</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/quick-burst-41477">
          <img alt="Icon skillengineer quick burst.36" src="/assets/icons/icon_skillengineer_quick_burst.36.png" />
        </a> <a href="/spells/quick-burst-41477">Quick Burst</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 6</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/energy-auger-34426">
          <img alt="Icon skillengineer energy trail.36" src="/assets/icons/icon_skillengineer_energy_trail.36.png" />
        </a> <a href="/spells/energy-auger-34426">Energy Auger</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 11</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/bio-shell-41341">
          <img alt="Icon skillengineer bioshell.36" src="/assets/icons/icon_skillengineer_bioshell.36.png" />
        </a> <a href="/spells/bio-shell-41341">Bio Shell</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 13</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/mortar-strike-41543">
          <img alt="Icon skillengineer mortar strike.36" src="/assets/icons/icon_skillengineer_mortar_strike.36.png" />
        </a> <a href="/spells/mortar-strike-41543">Mortar Strike</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 13</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/unstable-anomaly-34669">
          <img alt="Icon skillengineer anomaly launcher.36" src="/assets/icons/icon_skillengineer_anomaly_launcher.36.png" />
        </a> <a href="/spells/unstable-anomaly-34669">Unstable Anomaly</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 15</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/target-acquisition-38036">
          <img alt="Icon skillengineer target acquistion.36" src="/assets/icons/icon_skillengineer_target_acquistion.36.png" />
        </a> <a href="/spells/target-acquisition-38036">Target Acquisition</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 18 AMP</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/bolt-caster-34836">
          <img alt="Icon skillengineer bolt caster.36" src="/assets/icons/icon_skillengineer_bolt_caster.36.png" />
        </a> <a href="/spells/bolt-caster-34836">Bolt Caster</a>
      </td>
      <td class="center">Assault</td>
      <td>Level 24</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/bruiser-bot-42894">
          <img alt="Icon skillengineer bruiser bot.36" src="/assets/icons/icon_skillengineer_bruiser_bot.36.png" />
        </a> <a href="/spells/bruiser-bot-42894">Bruiserbot</a>
      </td>
      <td class="center">Support</td>
      <td>Level 4</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/flak-cannon-41426">
          <img alt="Icon skillengineer flak cannon.36" src="/assets/icons/icon_skillengineer_flak_cannon.36.png" />
        </a> <a href="/spells/flak-cannon-41426">Flak Cannon</a>
      </td>
      <td class="center">Support</td>
      <td>Level 9</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/hyper-wave-41275">
          <img alt="Icon skillengineer hyper wave.36" src="/assets/icons/icon_skillengineer_hyper_wave.36.png" />
        </a> <a href="/spells/hyper-wave-41275">Hyper Wave</a>
      </td>
      <td class="center">Support</td>
      <td>Level 9</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/unsteady-miasma-41484">
          <img alt="Icon skillengineer give em gas.36" src="/assets/icons/icon_skillengineer_give_em_gas.36.png" />
        </a> <a href="/spells/unsteady-miasma-41484">Unsteady Miasma</a>
      </td>
      <td class="center">Support</td>
      <td>Level 11</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/feedback-41864">
          <img alt="Icon skillengineer feedback.36" src="/assets/icons/icon_skillengineer_feedback.36.png" />
        </a> <a href="/spells/feedback-41864">Feedback</a>
      </td>
      <td class="center">Support</td>
      <td>Level 15</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/disruptive-module-41815">
          <img alt="Icon skillengineer disruptive mod.36" src="/assets/icons/icon_skillengineer_disruptive_mod.36.png" />
        </a> <a href="/spells/disruptive-module-41815">Disruptive Module</a>
      </td>
      <td class="center">Support</td>
      <td>Level 18 AMP</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/ricochet-41429">
          <img alt="Icon skillengineer ricochet.36" src="/assets/icons/icon_skillengineer_ricochet.36.png" />
        </a> <a href="/spells/ricochet-41429">Ricochet</a>
      </td>
      <td class="center">Support</td>
      <td>Level 18</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/shock-pulse-42585">
          <img alt="Icon skillengineer shock pulse.36" src="/assets/icons/icon_skillengineer_shock_pulse.36.png" />
        </a> <a href="/spells/shock-pulse-42585">Shock Pulse</a>
      </td>
      <td class="center">Support</td>
      <td>Level 21</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/particle-ejector-34658">
          <img alt="Icon skillengineer particule ejector.36" src="/assets/icons/icon_skillengineer_particule_ejector.36.png" />
        </a> <a href="/spells/particle-ejector-34658">Particle Ejector</a>
      </td>
      <td class="center">Support</td>
      <td>Level 24</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/thresher-52133">
          <img alt="Icon skillengineer thresher.36" src="/assets/icons/icon_skillengineer_thresher.36.png" />
        </a> <a href="/spells/thresher-52133">Thresher</a>
      </td>
      <td class="center">Support</td>
      <td>Level 31</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/zap-41438">
          <img alt="Icon skillengineer zap.36" src="/assets/icons/icon_skillengineer_zap.36.png" />
        </a> <a href="/spells/zap-41438">Zap</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 3</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/urgent-withdrawal-34522">
          <img alt="Icon skillengineer urgent withdrawal.36" src="/assets/icons/icon_skillengineer_urgent_withdrawal.36.png" />
        </a> <a href="/spells/urgent-withdrawal-34522">Urgent Withdrawal</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 6</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/recursive-matrix-42572">
          <img alt="Icon skillengineer recursive matrix.36" src="/assets/icons/icon_skillengineer_recursive_matrix.36.png" />
        </a> <a href="/spells/recursive-matrix-42572">Recursive Matrix</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 9</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/diminisher-bot-42833">
          <img alt="Icon skillengineer diminisher bot.36" src="/assets/icons/icon_skillengineer_diminisher_bot.36.png" />
        </a> <a href="/spells/diminisher-bot-42833">Diminisherbot</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 15</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/repair-bot-42810">
          <img alt="Icon skillengineer repair bot.36" src="/assets/icons/icon_skillengineer_repair_bot.36.png" />
        </a> <a href="/spells/repair-bot-42810">Repairbot</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 18 AMP</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/shatter-impairment-42803">
          <img alt="Icon skillengineer shatter impairment.36" src="/assets/icons/icon_skillengineer_shatter_impairment.36.png" />
        </a> <a href="/spells/shatter-impairment-42803">Shatter Impairment</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 18</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/volatile-injection-41756">
          <img alt="Icon skillengineer volatile injection.36" src="/assets/icons/icon_skillengineer_volatile_injection.36.png" />
        </a> <a href="/spells/volatile-injection-41756">Volatile Injection</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 21</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/code-red-41623">
          <img alt="Icon skillengineer code red.36" src="/assets/icons/icon_skillengineer_code_red.36.png" />
        </a> <a href="/spells/code-red-41623">Code Red</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 27</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/personal-defense-unit-44471">
          <img alt="Icon skillengineer personal defense unit.36" src="/assets/icons/icon_skillengineer_personal_defense_unit.36.png" />
        </a> <a href="/spells/personal-defense-unit-44471">Personal Defense Unit</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 27</td>
    </tr>
    <tr>
      <td class="name">
        <a class="icon" href="/spells/obstruct-vision-51605">
          <img alt="Icon skillengineer shock wave.36" src="/assets/icons/icon_skillengineer_shock_wave.36.png" />
        </a> <a href="/spells/obstruct-vision-51605">Obstruct Vision</a>
      </td>
      <td class="center">Utility</td>
      <td>Level 31</td>
    </tr>
  </tbody>
</table>

看起来有人打败了我,但这里有另一种做同样事情的方法。这次使用的是核心PHP函数。您可以使用DOMDocument解析文档,然后通过一些简单的DOMXPath查询获取请求的内容:

// Load the HTML into a DOM document (@ = suppress a bunch of warnings)
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.jabbithole.com/classes/engineer');
// Create a DOM XPath object
$xpath = new DOMXPath($dom);
// Find all <tr> tags within the first <table> element, skipping the first <tr>
// which is the header
$trs = $xpath->query('//table[1]//tr[position() > 1]');
// Loop through found <tr> tags    
$abilities = [];
foreach ($trs as $tr) {
    // Name is value of first <td> inside <tr>
    // Type is value of second <td> inside <tr>
    // Image is the src attribute of the <img> tag inside <tr>
    $name  = $xpath->query('.//td[1]',    $tr)->item(0)->nodeValue;
    $type  = $xpath->query('.//td[2]',    $tr)->item(0)->nodeValue;
    $image = $xpath->query('.//img/@src', $tr)->item(0)->nodeValue;
    // Add to result array
    $abilities[] = array(
        'name'  => trim($name),
        'type'  => trim($type),
        'image' => trim($image),
    );
}
print_r($abilities);

输出:

Array
(
    [0] => Array
        (
            [name] => Mode: Eradicate
            [type] => Innate
            [image] => /assets/icons/icon_skillengineer_eradication_mode.36.png
        )
    ...
    [31] => Array
        (
            [name] => Obstruct Vision
            [type] => Utility
            [image] => /assets/icons/icon_skillengineer_shock_wave.36.png
        )
)

使用此库:

<?PHP
    //include library
    require('simple_html_dom.php');
    $items = array('engineer');
    foreach($items as $key => $value) {
        $url = 'http://www.jabbithole.com/classes/'.$value;
        //read html from server and create html_dom object
        $html = file_get_html($url);
        //find the table using css selectors
        $table = $html->find('#tab-abi table', 0);
        $results = array();
        $first = true;
        //iterate each tr
        foreach($table->find('tr') as $element){
            //skip first tr (table header)
            if($first){
                $first = false;
                continue;
            }
            //extract data
            $results[] = array(
                'image' => $element->find('img',0)->src,
                'name' => $element->find('.name',0)->plaintext,
                'class' => $element->find('.center',0)->plaintext
            );
        }
    }
    print_r($results);
?>

该库允许您使用CSS选择器(jQuery样式)查询DOM,还支持格式错误的HTML。