在php中从网页上刮取纯文本

时间:2014-09-05 07:19:43

标签: php regex web-scraping

我尝试了大部分正则表达式。但他们不适合我.. 我需要正则表达式删除所有的html标签并返回值....在我的html文件中有以下html标签:输入文本,选择。            

          $file_string = file_get_contents('page_to_scrape.html');

          preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
          $title_out = $title[1];

          preg_match('/<option value="ELIT">(.*)<\/option>/i', $file_string,   $keywords);
          $keywords_out = $keywords[1];

          preg_match('/<option value="MAS" selected="selected">(.*)<\/option>/i', $file_string, $ash);
          $ash_s = $ash[1];

         preg_match('/<input type="text" value="(.*)"/>/i', $file_string, $description);
         $description_out = $description[1];

         preg_match_all('/<li><a href="(.*)">(.*)<\/a><\/li>/i', $file_string, $links);

        ?>

         <p><strong>Title:</strong> <?php echo $title_out; ?></p>
          <p><strong>Name:</strong> <?php echo $keywords_out; ?></p>
      <p><strong>TExtbox:</strong> <?php echo $description_out; ?></p>
     <p><strong>Event:</strong> <?php echo $ash_s; ?></p>
          <p><strong>Links:</strong> <em>(Name - Link)</em><br />
     <?php
            echo '<ol>';
           for($i = 0; $i < count($links[1]); $i++) {
              echo '<li>' . $links[2][$i] . ' - ' . $links[1][$i] . '</li>';
     }
       echo '</ol>';
        ?>
      </p>

Html文件                                                                                                           这是标题                                                                                                                                                                      

  • Link 1
  •                             
  • Link 2
  •                             
  • Link 3
  •                             
  • Link 4
  •                             
  • Link 5
  •                     </ul>
                        <div class="field">
                                    <label>Event:</label>
                                    <select name="event" class="event">
                                                                <option value="MAS" selected="selected">Same</option>
                                                                    <option value="ELIT">Same4</option>
                                                                    <option value="IPC">Same3</option>
                                                                    <option value="VLMW">Same2</option>
                                                        </select>
                                </div>
    
                                <div class="field">
                                                                <label class="sub">Surname:</label>
                                                    <input name="search[name]" value="Smith" type="text">
                                                    <br>
                                                                            <label class="sub">First Name:</label>
                                                    <input name="search[firstname]" value="Alex" type="text">
                                                    <br>
    
    
    
                                </div>
    
    
                        </div> 
                        </body> 
                        </html> 
    

    1 个答案:

    答案 0 :(得分:1)

    您可以使用DOM解析器,但为什么不保持简单。 HTML使用标签。这段代码获取所有文本,只有文本和基于数组的函数:

    $html = file_get_contents('http://stackoverflow.com/questions/25680536');
    
    $tags = explode('<',$html);
    
    foreach ($tags as $tag)
    {
      // skip scripts
      if (strpos($tag,'script') !== FALSE) continue;
      // get text
      $text = strip_tags('<'.$tag);
      // only if text present remember
      if (trim($text) != '') $texts[] = $text;
    }
    
    print_r($texts);
    

    它最终出现在一个数组中,这通常比纯文本更有用。你必须做更多的后清洁,但这是不可避免的。

    相关问题