Can you write a single-page PHP collection program and attach an instance? for example, I want to collect this page: news.163.com1206132083TJ7PA700014JB6.html. requirement: collect the title collection body. thank you! ------ Solution ------------------ first go to simplehtmldom. sourceforge. netindex to help write a single-page PHP collection program, and attach an instance
For example, I want to collect this page: http://news.163.com/12/0613/20/83TJ7PA700014JB6.html
Requirements:
Collection title
Collect Body
Thank you!
------ Solution --------------------
First go to the http://simplehtmldom.sourceforge.net/index.htm (click Download latest version form Sourceforge.) Download a simple_html_dom.php, silly regular expression, the other official website has a detailed tutorial, it is easy to understand.
Header ("Content-type: text/html; charset = gb2312 ");
Require dirname (_ FILE _). '/simple_html_dom.php ';
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // news.163.com/12/0613/20/83TJ7PA700014JB6.html ');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: 1.9.1.2) Gecko/20090729 Firefox/3.5.2 gtb5 ');
$ Htmls = curl_exec ($ ch );
Curl_close ($ ch );
$ Html = str_get_html ($ htmls );
Foreach ($ html-> find ('# h1title') as $ title ){
Echo strip_tags ($ title ).'
'; // Title
}
Foreach ($ html-> find ('# endtext') as $ content ){
Echo strip_tags ($ content); // Body
}
Note: curl simulates Mozilla browser crawling, which is faster than file_get_contents () parsing. Strip_tags () is used to remove the evil iframe advertisement. what you need is the redundant html tags of "collection Title" and "collection body", which is useless to you.
------ Solution --------------------
PHP:
This article describes how to use PHP to obtain QQ mail friends. PHP has a curl extension which is generally used to collect data. However, you can also perform simulated login, log on to your QQ mailbox through simulated login, and use curl to retrieve the list of friends in your mailbox. The above is the implementation principle. after understanding Yuan Li, let's look at the implementation process. The specific PHP code for getting QQ mail friends is as follows:
1. 2. class QQHttp {
3. var $ cookie = '';
4. function _ cunstrut (){
5 .}
6. function makeForm (){
7. $ form = array (
8. 'URL' => "http://mail.qq.com/cgi-bin/loginpage ",
9 .);
10. $ data = $ this-> curlFunc ($ form );
11. preg_match ('/name = "ts" svalue = "(d +)"/', $ data ['HTML '], $ tspre );
12. $ ts = $ tspre [1];
13. preg_match ('/action = "http: // (md +) .mail.qq.com/', using data='html'], $ server );
14. $ server_no = $ server [1];
15./* login.html loading */
16. $ html = file_get_contents (dirname (_ FILE _). '/login.htm ');
17. $ html = str_replace ('{_ ts _}', $ ts, $ html );
18. $ html = str_replace ('{_ server_no _}', $ server_no, $ html );
19. return $ html;
20 .}
21. function curlFunc ($ array)
22 .{
23. $ ch = curl_init ();
24. curl_setopt ($ ch, CURLOPT_URL, $ array ['URL']);