Lấy dữ liệu từ URL hoặc file một cách đơn giản, code ngắn gọn dễ hiểu, có file demo đính kèm. Tôi sử dụng thư viện simple html dom để trí...
Lấy dữ liệu từ URL hoặc file một cách đơn giản, code ngắn gọn dễ hiểu, có file demo đính kèm.
Tôi sử dụng thư viện simple html dom để trích xuất các dữ liệu từ trang nguồn, các bạn xem thư viện tại http://simplehtmldom.sourceforge.net/.
Để cụ thể tôi sẽ trích xuất dữ liệu từ đường link http://ngoisao.net/tin-tuc/showbiz-viet/2013/07/mr-dam-mang-thi-sinh-giong-hat-viet-2013-ra-san-khau-lon-246330/. Các thông tin tôi sẽ lấy bao gồm tiêu đề tin, phần tóm tắt và phần miêu tả.
Các tham số các bạn sẽ phải xác định là:
require_once “simple_html_dom.php”;
$link = “http://ngoisao.net/tin-tuc/showbiz-viet/2013/07/mr-dam-mang-thi-sinh-giong-hat-viet-2013-ra-san-khau-lon-246330/”;
$html = file_get_html($link);
Để xác định mẫu cần lấy các bạn sử dụng Firebug hoặc view source để xem cấu trúc trang, tìm tới vùng dữ liệu cần lấy.
Với phần tiêu đề tin các bạn sẽ thấy mẫu cần tìm là:
$title_pattern = “h1.newsdetail-title”;
Với phần tóm tắt mẫu sẽ là:
$brief_pattern = “p.newsdetail-brief”;
Với phần miêu tả sẽ là:
$description_pattern = “div.newsdetail-description”;
Trong phần miêu tả có những phần nội dung ta không muốn lấy vậy ta khai báo các mẫu cần xóa trong phần miêu tả:
$description_pattern_delete = “div.topDetail,h1.Title,h2.Lead,p.RelatedLeadSubject,div.detailNS,div.relateNewsDetail”;
Giờ ta khai báo một mảng chứa các thông tin lấy về:
$item=array();
Lấy tiêu đề
foreach($html->find($title_pattern) as $element){
$item[‘title’] = trim($element->plaintext);
}
Lấy tóm tắt
foreach($html->find($brief_pattern) as $element){
$item[‘brief’] = trim($element->plaintext);
}
Lấy miêu tả
foreach($html->find($description_pattern) as $element){
// Xóa các mẫu trong miêu tả
if($description_pattern_delete){
$arr = explode(‘,’,$description_pattern_delete);
for($i=0;$i<count($arr);$i++){
foreach($element->find($arr[$i]) as $e){
$e->outertext=”;
}
}
}
$item[‘description’] = $element->innertext; // Lấy toàn bộ phần html
// Bổ sung đường dẫn ảnh
if(isset($item[‘description’]) and $item[‘description’]){
$item[‘description’]=str_replace(“/Files/”,”http://ngoisao.net/Files/”,$item[‘description’]);
}
}
Vậy là đã xong phần code PHP để lấy tin từ một trang cụ thể. Tôi gửi phần code trong file đính kèm để các bạn test thử.
Trong bài http://minhtc.net/lay-tin-tuc-tu-dong-tu-website-khac-bang-php.html tôi đã viết sẵn một project để lấy tin từ các website thương mại điện tử. Project này cho phép người sử dụng tự khai báo mẫu lấy tin cho từng site riêng biệt mà không phải đụng vào code, lấy nhiều tin ở nhiều site cùng lúc và nhiều tùy chọn nữa.
Bạn nào sử dụng mã nguồn WordPress có thể tham khảo: http://minhtc.net/lay-tin-tu-dong-wordpress-feednews.html
Bạn nào sử dụng mã nguồn Nukeviet có thể tham khảo: http://minhtc.net/lay-tin-tu-dong-module-feednews-nukeviet-ban-co-phi.html
Chúc các bạn thành công!
Nếu thấy có ích các bạn nhớ ấn LIKE như một lời cảm ơn tác giả nhé
Tác giả: minhtc.net
Để cụ thể tôi sẽ trích xuất dữ liệu từ đường link http://ngoisao.net/tin-tuc/showbiz-viet/2013/07/mr-dam-mang-thi-sinh-giong-hat-viet-2013-ra-san-khau-lon-246330/. Các thông tin tôi sẽ lấy bao gồm tiêu đề tin, phần tóm tắt và phần miêu tả.
Các tham số các bạn sẽ phải xác định là:
- Đường dẫn tin cần lấy (http://ngoisao.net/tin-tuc/showbiz-viet/2013/07/mr-dam-mang-thi-sinh-giong-hat-viet-2013-ra-san-khau-lon-246330/)
- Mẫu lấy tiêu đề tin
- Mẫu lấy phần tóm tắt
- Mẫu lấy phần miêu tả
require_once “simple_html_dom.php”;
$link = “http://ngoisao.net/tin-tuc/showbiz-viet/2013/07/mr-dam-mang-thi-sinh-giong-hat-viet-2013-ra-san-khau-lon-246330/”;
$html = file_get_html($link);
Để xác định mẫu cần lấy các bạn sử dụng Firebug hoặc view source để xem cấu trúc trang, tìm tới vùng dữ liệu cần lấy.
Với phần tiêu đề tin các bạn sẽ thấy mẫu cần tìm là:
$title_pattern = “h1.newsdetail-title”;
Với phần tóm tắt mẫu sẽ là:
$brief_pattern = “p.newsdetail-brief”;
Với phần miêu tả sẽ là:
$description_pattern = “div.newsdetail-description”;
Trong phần miêu tả có những phần nội dung ta không muốn lấy vậy ta khai báo các mẫu cần xóa trong phần miêu tả:
$description_pattern_delete = “div.topDetail,h1.Title,h2.Lead,p.RelatedLeadSubject,div.detailNS,div.relateNewsDetail”;
Giờ ta khai báo một mảng chứa các thông tin lấy về:
$item=array();
Lấy tiêu đề
foreach($html->find($title_pattern) as $element){
$item[‘title’] = trim($element->plaintext);
}
Lấy tóm tắt
foreach($html->find($brief_pattern) as $element){
$item[‘brief’] = trim($element->plaintext);
}
Lấy miêu tả
foreach($html->find($description_pattern) as $element){
// Xóa các mẫu trong miêu tả
if($description_pattern_delete){
$arr = explode(‘,’,$description_pattern_delete);
for($i=0;$i<count($arr);$i++){
foreach($element->find($arr[$i]) as $e){
$e->outertext=”;
}
}
}
$item[‘description’] = $element->innertext; // Lấy toàn bộ phần html
// Bổ sung đường dẫn ảnh
if(isset($item[‘description’]) and $item[‘description’]){
$item[‘description’]=str_replace(“/Files/”,”http://ngoisao.net/Files/”,$item[‘description’]);
}
}
Vậy là đã xong phần code PHP để lấy tin từ một trang cụ thể. Tôi gửi phần code trong file đính kèm để các bạn test thử.
Trong bài http://minhtc.net/lay-tin-tuc-tu-dong-tu-website-khac-bang-php.html tôi đã viết sẵn một project để lấy tin từ các website thương mại điện tử. Project này cho phép người sử dụng tự khai báo mẫu lấy tin cho từng site riêng biệt mà không phải đụng vào code, lấy nhiều tin ở nhiều site cùng lúc và nhiều tùy chọn nữa.
Bạn nào sử dụng mã nguồn WordPress có thể tham khảo: http://minhtc.net/lay-tin-tu-dong-wordpress-feednews.html
Bạn nào sử dụng mã nguồn Nukeviet có thể tham khảo: http://minhtc.net/lay-tin-tu-dong-module-feednews-nukeviet-ban-co-phi.html
Chúc các bạn thành công!
Nếu thấy có ích các bạn nhớ ấn LIKE như một lời cảm ơn tác giả nhé
Tác giả: minhtc.net
COMMENTS