Hướng dẫn viết code lấy tin tự động bằng php

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à:
  1. Đườ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/)
  2. Mẫu lấy tiêu đề tin
  3. Mẫu lấy phần tóm tắt
  4. Mẫu lấy phần miêu tả
Trước hết các bạn chèn thư viện simple_html_dom.php vào:
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/&#8221;,$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

Tên

Bất động sản,80,Ebook,1,Giá cả thị tường,1,Hài,4,Marketing,10,Thế giới đó đây,4,Thị trường,1,Thiết kế web,4,Thời sự,8,Tin học,26,Tin tức,5,Tuyển dụng,4,Tự động hóa,6,VR360,1,
ltr
item
Số hóa tin tức: Hướng dẫn viết code lấy tin tự động bằng php
Hướng dẫn viết code lấy tin tự động bằng php
Số hóa tin tức
https://sohoatintuc.blogspot.com/2015/03/huong-dan-viet-code-lay-tin-tu-ong-bang.html
https://sohoatintuc.blogspot.com/
http://sohoatintuc.blogspot.com/
http://sohoatintuc.blogspot.com/2015/03/huong-dan-viet-code-lay-tin-tu-ong-bang.html
true
3523791636043463033
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content