使用简单的HTML DOM操作HTML数据

时间:2013-07-09 09:54:53

标签: php web-scraping simple-html-dom

我有一些HTML,我试图插入到数据库中,但事实证明它比我想象的更难。

HTML看起来像那样(id值是静态的,每个问题都有id78questionRegion作为id):

<div class="fullquestion">
<div class='question'>House Frey are Bannermen sworn to which House?</div><div style=" visibility:visible; " id="id78questionRegion">


<a href="javascript:___gid_10(0)"><span>House Baratheon</span></a>

<a href="javascript:___gid_10(1)"><span>House Tyrell</span></a>

<a href="javascript:___gid_10(2)"><span>House Arryn</span></a>

<a href="javascript:___gid_10(3)"><span>House Stark</span></a>

<a href="javascript:___gid_10(4)"><span>House Tully</span></a>

</div><div class='answer'>House Tully</div>
</div>

和PHP

 <?php

 $type = '';
 require ($_SERVER["DOCUMENT_ROOT"] . '/inc/db.php');
 require($_SERVER["DOCUMENT_ROOT"] . '/admin/simple_html_dom.php');

 $file = 'qa.html';
 $html = new simple_html_dom();
 $html->load_file($file);
 $qcount = 100;
 for ($i = 0; $i <= $qcount; $i++) {
  echo 'Question is:<br>';
  echo $html->find('.question', $i);
  echo 'Possible Answers are:<br>';
  foreach ($html->find('#id78questionRegion a span', $i) as $question) {
   echo $html->find('#id78questionRegion a span');
  }

  echo 'Answer is:<br>';
  echo $html->find('.answer', $i);
 }
 ?>

我想要提出问题,var中的每个可能的答案以及正确的答案,但我无法弄清楚如何正确地得到答案。任何帮助表示赞赏!

感谢。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

foreach ($html->find('.fullquestion') as $fq) {
  echo 'Question is: <br />';
  $question = $fq->find('.question');
  echo $question->innertext . '<br />\n';
  echo 'Possible answers are: <br \>';
  foreach ($fq->find('span') as $a) {
    echo $a->innertext . '<br />\n';
  }
  echo 'Correct answer is: <br \>';
  $answer = $fq->find('.answer');
  echo $answer->innertext . "<br /><br />\n";
}