PHP Simple HTML DOM Parser
페이지 정보
본문
기본적으로 url 에서 가져올 때는 file_get_html 를 이용해서 가져온다.
그런데, 가져올 때 잘못가져오거나 덜 가져오는 경우가 있다.
이때는 php 기본함수인 file_get_contents 를 이용해서 가져온 후에,
html 문서를 str_get_html 함수로 DOM 을 읽어 올 수 있다.
아래와 같은 것들로 불러올 수 있다.
innerhtml
innertext
plaintext : 태그를 제외한 문자열만
## How to get HTML elements?
```
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
```
## How to modify HTML elements?
```
// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
```
## Extract contents from HTML
```
// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->plaintext;
```
## Scraping Slashdot!
```
// Create DOM from URL
$html = file_get_html('http://slashdot.org/');
// Find all article blocks
foreach($html->find('div.article') as $article) {
$item['title'] = $article->find('div.title', 0)->plaintext;
$item['intro'] = $article->find('div.intro', 0)->plaintext;
$item['details'] = $article->find('div.details', 0)->plaintext;
$articles[] = $item;
}
print_r($articles);
```
## Finding descendants
```
// Find all <li> in <ul>
$es = $html->find('ul li');
// Find Nested <div> tags
$es = $html->find('div div div');
// Find all <td> in <table> which class=hello
$es = $html->find('table.hello td');
// Find all td tags with attribite align=center in table tags
$es = $html->find('table td[align=center]');
```
** 참고
http://simplehtmldom.sourceforge.net
https://simplehtmldom.sourceforge.io/docs/1.9/manual/finding-html-elements/
그런데, 가져올 때 잘못가져오거나 덜 가져오는 경우가 있다.
이때는 php 기본함수인 file_get_contents 를 이용해서 가져온 후에,
html 문서를 str_get_html 함수로 DOM 을 읽어 올 수 있다.
아래와 같은 것들로 불러올 수 있다.
innerhtml
innertext
plaintext : 태그를 제외한 문자열만
## How to get HTML elements?
```
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
```
## How to modify HTML elements?
```
// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
```
## Extract contents from HTML
```
// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->plaintext;
```
## Scraping Slashdot!
```
// Create DOM from URL
$html = file_get_html('http://slashdot.org/');
// Find all article blocks
foreach($html->find('div.article') as $article) {
$item['title'] = $article->find('div.title', 0)->plaintext;
$item['intro'] = $article->find('div.intro', 0)->plaintext;
$item['details'] = $article->find('div.details', 0)->plaintext;
$articles[] = $item;
}
print_r($articles);
```
## Finding descendants
```
// Find all <li> in <ul>
$es = $html->find('ul li');
// Find Nested <div> tags
$es = $html->find('div div div');
// Find all <td> in <table> which class=hello
$es = $html->find('table.hello td');
// Find all td tags with attribite align=center in table tags
$es = $html->find('table td[align=center]');
```
** 참고
http://simplehtmldom.sourceforge.net
https://simplehtmldom.sourceforge.io/docs/1.9/manual/finding-html-elements/
추천0
댓글목록
등록된 댓글이 없습니다.