PHP uses simple_html_dom to parse HTML example

WBOY
Release: 2016-07-27 16:56:17
Original
1118 people have browsed it

The example in this article describes how PHP uses simple_html_dom to parse HTML. I share it with you for your reference. The details are as follows:

I wrote two crawlers today, one using Python and one using PHP. To be honest, both implementation methods are very convenient.

Let’s talk about using the open source class simple_html_dom. Parse HTML code:

1. Download simple_html_dom:

http://sourceforge.net/projects/simplehtmldom/files/

or click here to download from this site.

2. Usage examples:

<&#63;php
// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.google.com/');
// find all link
foreach($html->find('a') as $e) 
  echo $e->href . '<br>';
// find all image
foreach($html->find('img') as $e)
  echo $e->src . '<br>';
// find all image with full tag
foreach($html->find('img') as $e)
  echo $e->outertext . '<br>';
// find all div tags with id=gbar
foreach($html->find('div#gbar') as $e)
  echo $e->innertext . '<br>';
// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
  echo $e->outertext . '<br>';
// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
  echo $e->innertext . '<br>';
// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
// extract text from HTML
echo $html->plaintext;
?>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Coding and Transcoding Operation Skills", "Introduction Tutorial on PHP Object-Oriented Programming", " Summary of PHP Mathematical Operation Skills", "Comprehensive Collection of PHP Array Operation Skills", "Summary of PHP String Usage", "PHP Data Structure and Algorithm Tutorial", "Summary of PHP Programming Algorithms", "php Regular Expression "Summary of Formula Usage" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

The above introduces the example of using simple_html_dom to parse HTML in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template