glitch in PHP Simple HTML DOM parser

前端 未结 4 1929
既然无缘
既然无缘 2021-01-12 05:38

I\'m using PHP Simple HTML DOM Parser to scrape some data of a webshop (also running XAMPP 1.7.2 with PHP5.3.0), and I\'m running into problems with

相关标签:
4条回答
  • 2021-01-12 05:53

    Make sure that tbody is really is there. Many browsers will add a tbody to tables in the inspect panel even though they are not present in the response.

    0 讨论(0)
  • 2021-01-12 05:59

    Make sure your tbody is coming from some javascript execution. I was facing the same problem with a span tag. Later I found that, if any html code is getting into the page via jquery/any other javascript execution then in that case simple_html_dom simply fails.

    0 讨论(0)
  • 2021-01-12 06:07

    in simple_html_dom.php file comment or remove line #396

    // if ($m[1]==='tbody') continue;
    
    0 讨论(0)
  • 2021-01-12 06:14

    There is a bug report for this issue here: http://sourceforge.net/p/simplehtmldom/bugs/79/

    It is still open at the time of this writing. There is an alternative fix if you do not wish to modify the source code, for example in a loop to find <tr>'s

    <?php
      // The *BROKEN* way to find the <tr>'s 
      // below the <tbody> below the <table id="foo">
      foreach($dom->find('tbl#foo tbody tr') as $tr) {
        /* you will get nothing */
      }
    

    You can instead selectively check the parent tag name while iterating all <tr>'s like so:

    <?php
      // A workaround to find the <tr>'s 
      // below the <tbody> below the <table id="foo">
      foreach($dom->find('tbl#foo tr') as $tr) { // note the lack of tbody selector
        /* you will get all trs, but let's only work with ones with the parent
           of a tbody! */
        if($tr->parent->tag == 'tbody') { // our workaround
          /* this part will work as you would expect the above broken code to work */
        }
      }
    

    Also note, a slightly unrelated issue that I ran into, that Chrome and FF inspectors will correct tag soup regarding<tbody> and <thead>. Be careful -- only look at the actual source -- stay away from the DOM inspectors if you run into unexplainable issues.

    0 讨论(0)
提交回复
热议问题