1
<tr class='Jed01'>
<td height='20' class='JEDResult'>1</td>
<td height='30' class='JEDResult'>26.04.2013</td>
<td height='30' class='JEDResult'>19:43</td>
<td height='30' class='JEDResult'>Processing</td>
<td height='30' class='JEDResult'><a href="#" pressed="GetInfo(1233);" title=''>Jeddah</a></td>
</tr>

結果 = 最初のステップ - 日付 - 時間 - 状態 - 場所

まず第一に、私はPHPが初めてで、このデータをPHP経由でWebに解析しようとしています-DOMは以前にStackoverflowで推奨されていました。以下のコードでは、すべてのクラスを呼び出してデータを取得しましたが、問題がないのに結果を取得できません。それで、それが私の問題である可能性がありますか?

今からありがとう

<?php

$input = "www.kalkatawi.com/luai.html"
$html = new DOMDocument();
$html->loadHTML($input);


foreach($html->getElementsByTagName('tr') as $tr)
{
  if($tr->getAttribute('class') == 'Jed01')
  {
    foreach($html->getElementsByTagName('td') as $td)
    {
      if($td->getAttribute('class') == 'JEDResult')
      {
        echo ($td->nodeValue);
      }
    }     
  }
}

?>
4

2 に答える 2

2

これらのセミコロンを忘れないでください ;)

これを試して;

<?php

$input = file_get_contents("http://www.kalkatawi.com/luai.html");
$html = new DOMDocument();
$html->loadHTML($input);


foreach($html->getElementsByTagName('tr') as $tr)
{
  if($tr->getAttribute('class') == 'Jed01')
  {
    foreach($tr->getElementsByTagName('td') as $td)
    {
      if($td->getAttribute('class') == 'JEDResult')
      {
        echo ($td->nodeValue);
        echo '<br/>';
      }
    }     
  }
  echo '<br/><br/>';
}

?>

出力する必要があります。

1
26.04.2013
19:43
Processing
Jeddah


2
26.04.2013
20:43
Printed
RIY
于 2013-04-26T09:18:27.293 に答える
1

このコードにはいくつかの問題があります。

HTML のロード

$input = 'MyLink';
$html = new DOMDocument();
$html->loadHTML($input);

このコードは文字列を HTML として処理しようとし'MyLink'ますが、明らかにそうではありません。それが実際のコードである場合、この時点を超えて何も機能しません。適切な HTML 入力を提供するかloadHTMLFile、ファイルから HTML をロードするために使用します。

比較では大文字と小文字が区別されます

一方では、これがあります:

<tr class='Jed01'>

そしてもう一方にこれ:

if($tr->getAttribute('class') == 'JED01')

'Jed01'!=であるため'JED01'、これは決してtrue. 大文字と小文字を修正するかstricmp、クラスを比較するなどの他のメカニズムを使用してください。

オブジェクトを印刷できません

これにより、致命的なエラーが発生します。

echo ($td);

代わりにどうあるべきか: 最も可能性が高いecho $td->nodeValueですが、やりたいことに応じて他の可能性が開かれています。

しかし、XPath を使用すると、はるかに簡単に実行できます。

$xpath = new DOMXPath($html);
$query = "//tr[@class='Jed01']//td[@class='JEDResult']"; // google XPath syntax

foreach ($xpath->query($query) as $node) {
    print_r($node->nodeValue);
}
于 2013-04-26T09:25:49.137 に答える