This article illustrates the method of crawling and analyzing Web pages in PHP. Share to everyone for your reference, specific as follows:
Capturing and analyzing a file is a very simple thing to do. This tutorial will take you through an example to achieve it step-by-step. Let's get started!
First of all, I must first decide which URL address we will crawl. Can be passed in script or through $query_string. For simplicity's sake, let's set the variables directly in the script.
<?php
$url = ' http://www.php.net ';
? >
In the second step, we crawl the specified file and we use the file () function to put it in an array.
<?php
$url = ' http://www.php.net ';
$lines _array = file ($url);
? >
OK, now we have the file in the array. However, the text we want to analyze may not be all in one line. To solve this file, we can simply convert the array $lines_array into a string. We can use the implode (x,y) function to implement it. If you want to use explode (the array of string variables) in the back, set X to "|" or "!" or other similar delimiters might be better. But for our purposes, it is best to set the X as a space. Y is another necessary parameter because it is an array you want to process with implode ().
<?php
$url = ' http://www.php.net ';
$lines _array = file ($url);
$lines _string = Implode (', $lines _array);
? >
Now, the crawl work is done, the following analysis. For the purposes of this example, we want everything between
<?php
$url = ' http://www.php.net ';
$lines _array = file ($url);
$lines _string = Implode (", $lines _array);
Eregi ("
Let's take a look at the code. As you can see, the eregi () function is executed in the following format:
"(. *)" means everything, which can be interpreted as "analyzing the things between
Finally, we can lose data. Because there is only one instance between
<?php
$url = ' http://www.php.net ';
$lines _array = file ($url);
$lines _string = Implode (", $lines _array); Eregi ("
That's the whole code.
<?php//Get all content URLs saved to file function Get_index ($save _file, $prefix = "Index_") {$count = 68;
$i = 1;
if (file_exists ($save _file)) @ unlink ($save _file); $fp = fopen ($save _file, "A +") or Die ("Open"). $save _file.
"Failed"); while ($i < $count) {$url = $prefix. $i.
". htm"; echo "Get". $url.
"..." ;
$url _str = Get_content_url (Get_url ($url));
echo "ok/n";
Fwrite ($fp, $url _str);
+ + $i;
} fclose ($FP); //Get Target Multimedia object function Get_object ($url _file, $save _file, $split = "| |:* *:--|") {if (! file_exists ($url _file)) die ($url _file.
"Not exist");
$file _arr = file ($url _file); if (! Is_array ($file _arr) | | | empty ($file _arr)) die ($url _file.
"Not content");
$url _arr = Array_unique ($file _arr);
if (file_exists ($save _file)) @ unlink ($save _file); $fp = fopen ($save _file, "A +") or Die ("Open save File"). $save _file.
"Failed"); foreach ($url _arr as $url) {if (EM)Pty ($URL)) continue; echo "Get". $url.
"..." ;
$html _str = Get_url ($url);
echo $html _str;
echo $url;
Exit
$obj _str = get_content_object ($html _str);
echo "ok/n";
Fwrite ($fp, $obj _str);
} fclose ($FP);
///Traversal directory Fetch file content function Get_dir ($save _file, $dir) {$DP = Opendir ($dir);
if (file_exists ($save _file)) @ unlink ($save _file); $fp = fopen ($save _file, "A +") or Die ("Open save File"). $save _file.
"Failed"); while (($file = Readdir ($DP))!= false) {if ($file!= "." && $file!= "...") {echo ' Read file '. $file.
"..." ;
$file _content = file_get_contents ($dir. $file);
$obj _str = get_content_object ($file _content);
echo "ok/n";
Fwrite ($fp, $obj _str);
} fclose ($FP);
}//Get the specified URL content function Get_url ($url) {$reg = '/^http:////[^//].+$/'; if (! Preg_match ($reg, $url)) Die ($url.
"Invalid"); $fp = fopen ($url, "R") Or Die ("Open URL:".) $url.
"failed.");
while ($FC = Fread ($fp, 8192)) {$content. = $FC;
} fclose ($FP); if (empty ($content)) {die ("Get URL:".) $url.
"Content failed.");
return $content; ///Use socket to get the specified Web function get_content_by_socket ($url, $host) {$fp = Fsockopen ($host,) or Die ("Open"). $url.
"Failed"); $header = "Get/". $url.
"http/1.1/r/n";
$header. = "Accept: */*/r/n";
$header. = "accept-language:zh-cn/r/n";
$header. = "Accept-encoding:gzip, deflate/r/n"; $header. = "user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon;
Infopath.1. NET CLR 2.0.50727)/r/n "; $header. = "Host:". $host.
"/r/n";
$header. = "connection:keep-alive/r/n"; $header. = "cookie:cnzz02=2; rtime=1; ltime=1148456424859;
cnzz_eid=56601755-/r/n/r/n ";
$header. = "connection:close/r/n/r/n";
Fwrite ($fp, $header); while (! feof ($fp)) {$contents. = fgets ($fp, 8192 );
} fclose ($FP);
return $contents; }//Get URL function get_content_url ($host _url, $file _contents) {//$reg = '/^ (#|<a href=) in the specified content http://lib.csdn.net /base/18 "class= ' Replace_word ' title=" javascript Knowledge Base "target= ' _blank ' style= ' color: #df3434; Font-weight:bold; ' >javascript</a>.*?| ftp:////.+|http:////.+|. *?href.*?| play.*?| index.*?|.
*?asp) +$/i '; $reg = '/^ (down.*?/.html|/d+_/d+/.htm.*?)
$/i ';
$rex = "/([HH][RR][EE][FF])/s*=/s*['/"]* ([^> '/"/s]+) [/' >]*/s*/i ';
$reg = '/^ (down.*?/.html) $/i ';
Preg_match_all ($rex, $file _contents, $r); $result = "";
Array (); foreach ($r as $c) {if (Is_array ($c)) {foreach ($c as $d) {if (Preg_match ($reg)) {$d . = $host _url. $d. "/n";
return $result; //Gets the multimedia file function get_content_object in the specified content ($str, $split = "| |:* *:--|")
{$regx = "/href/s*=/s*['/"]* ([^> '/"/s]+) [/" ' >]*/s* (.*?<//b>)/I "; Preg_match_all ($REGX, $stR, $result);
if (count ($result) = = 3) {$result [2] = Str_replace ("Multimedia:", "", $result [2]);
$result [2] = Str_replace ("", "", $result [2]); $result = $result [1] [0]. $split. $result [2] [0].
"/n";
return $result;
}?>
More about PHP Interested readers can view the site topics: "PHP Regular Expression Usage Summary", "Php+ajax Skills and Application Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", " PHP Operations Office Document Tips summary (including word,excel,access,ppt), "PHP Date and Time usage summary", "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.