Crawl dữ liệu từ một website sử dụng php
Trong bài viết này AnonyHome sẽ hướng dẫn các bạn lấy dữ liệu từ 1 website khác bằng cách sử dụng thư viện trong php. Và thư viện AnonyHome giới thiệu hôm nay đó là PHP Simple HTML DOM Parser.
Giới thiệu về php Simple HTML DOM Parser
Hiểu đơn giản PHP Simple HTML DOM Parser là một thư viện của PHP giúp chúng ta có thể lấy các thuộc tính của các thẻ HTML trong 1 website rất dễ dàng. Nó hỗi trợ một số tính năng như hỗ trợ bóc tách dữ liệu theo từng class, id hoặc theo các thẻ img, a, table..
Cài đặt
Để cài đặt thư viện này các bạn có thể truy cập vào trang chủ của nó theo đường dẫn sau http://simplehtmldom.sourceforge.net/ để tải về. Sau khi đã có thư viện các bạn có thể copy file simple_html_dom.php vào dự án của mình và include file đó vào file bạn muốn lấy dữ liệu để sử dụng.
Các bạn có thể xem các phương thức của thư viện simple_html_dom.php tại đây http://simplehtmldom.sourceforge.net/manual.htm/
Demo một số phương thức của simple_html_dom.php
Ví dụ 1. Hiển thị toàn bộ nội dung trang Cr- Thắng Blog.
1 |
<span style="box-sizing: inherit;"><span style="box-sizing: inherit; color: #61aeee;"><?php</span> <br /> <span style="box-sizing: inherit; color: #c678dd;">include</span>(<span style="box-sizing: inherit; color: #98c379;">'simple_html_dom.php'</span>);<br /> $url = <span style="box-sizing: inherit; color: #98c379;">'https://crthang.blogspot.com/'</span>;<br /> $html = file_get_html($url);<br /> <span style="box-sizing: inherit; color: #c678dd;">echo</span> $html;<br /><span style="box-sizing: inherit; color: #61aeee;">?></span></span><br /> |
Kết quả nhận được:
Ví dụ 2. Lấy ảnh từ trang Cr- Thắng Blog trên
1 |
<span style="box-sizing: inherit;"><span style="box-sizing: inherit; color: #61aeee; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit; letter-spacing: normal;"><?php</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"> <br /> </span><span style="box-sizing: inherit; color: #c678dd; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit; letter-spacing: normal;">include</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;">(</span><span style="box-sizing: inherit; color: #98c379; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit; letter-spacing: normal;">'simple_html_dom.php'</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;">);<br /> $url = </span><span style="box-sizing: inherit; color: #98c379; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: 14.4px; letter-spacing: normal;">'https://crthang.blogspot.com/'</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: 14.4px; letter-spacing: normal;">;</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><br /> $html = file_get_html($url);<br /> </span><span style="color: #c678dd; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><span style="font-size: 14.4px;">foreach($html->find('img') as $element) {<br /> echo '<img src="'.$element->src.'" /><br>';<br />}</span></span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><br /></span><span style="box-sizing: inherit; color: #61aeee; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit; letter-spacing: normal;">?></span></span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><br /></span> |
Kết quả nhận được:
Ví dụ 3. Lấy các link từ Cr- Thắng Blog
1 2 |
<span style="box-sizing: inherit; color: #61aeee; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;"><?php</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;"> <br /> </span><span style="box-sizing: inherit; color: #c678dd; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;">include</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;">(</span><span style="box-sizing: inherit; color: #98c379; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;">'simple_html_dom.php'</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;">);<br /> $url = </span><span style="box-sizing: inherit; color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: 14.4px;">'https://crthang.blogspot.com/'</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: 14.4px;">;</span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;"><br /> $html = file_get_html($url);<br /></span><span style="color: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><span style="font-size: 14.4px;"> foreach($html->find('a') as $element) <br /> echo $element->href . '<br>'; </span><br /></span><span style="box-sizing: inherit; color: #61aeee; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace; font-size: inherit;">?></span><span style="col or: white; font-family: "menlo" , "monaco" , "consolas" , "liberation mono" , "courier new" , monospace;"><br /></span> |
kết quả nhận được:
Kết luận
Như vậy trong bài viết này AnonyHome đã giới thiệu cho các bạn cách lấy dữ liệu từ một website thông qua thư viện simple_html_dom.php . Hi vọng nó sẽ ít nhiều giúp ích cho các bạn trong thực tế.