- function Match_links ($document) {
- Preg_match_all ("' <\s*a\s.*?href\s*=\s* ([\ ' \ '])?" (1) (.*?) \\1| ([^\s\>]+)) [^>]*>? (.*?)' Isx ", $document, $links);
- while (list ($key, $val) = each ($links [2])) {
- if (!empty ($val))
- $match [' link '] [] = $val;
- }
- while (list ($key, $val) = each ($links [3])) {
- if (!empty ($val))
- $match [' link '] [] = $val;
- }
- while (list ($key, $val) = each ($links [4])) {
- if (!empty ($val))
- $match [' content '] = $val;
- }
- while (list ($key, $val) = each ($links [0])) {
- if (!empty ($val))
- $match [' All '] [] = $val;
- }
- return $match;
- }
Copy Codeis mainly a regular problem, the following gives an ASP, the multi-Test regular Fetch page link regular
- public string Gethref (string htmlcode)
- {
- String matchvale = "";
- String Reg = @ "(h| H) (r| R) (e| E) (f| F) *= * (' | "")? ((\w|\\|\/|\.|:| -|_) +) (' | ' "| *|>)?";
- foreach (Match m in Regex.Matches (Htmlcode, Reg))
- {
- Matchvale + = (m.value). ToLower (). Replace ("href=", ""). Trim () + "| |";
- }
- return matchvale;
- }
Copy CodeExample 2,php function code for downloading remote pictures in content with regular expressions Using the PHP regular expression to determine the content of the picture, download and save the picture under the domain name program is actually a "thief program" an important part. This section of the program is just the section that downloads remote images.
- if (Preg_match_all ("/http://[^" ']+[.jpg|. Gif|. Jpeg|. Png]+/ui ", Stripcslashes ($content), $aliurl)) {
- $i = 0; Multiple files + +
- while (list ($key, $v) = each ($aliurl [0])) {
- echo $v. "
";
- $filetype = PathInfo ($v, pathinfo_extension); Get suffix Name
- $FF = @file_get_contents ($v); Get 2 binary file contents
- if (!stripos ($v, "jbxue.com")) {//Determine if it is a picture under your own website
- if (!empty ($FF)) {//Get to file do the following
- $dir = "upload/". Date ("Ymd"). " /";//Specify a new storage path
- if (!file_exists ($dir)) {//Determine if the directory exists
- @mkdir ($dir, 511,true); Create multi-level catalogs, 511 converted to decimal 777 with executable permissions
- }//Bbs.it-home.org
- $NFN = $dir. Date ("Ymdhis"). $i. ".". $filetype; Build a new name for the file
- $NF = @fopen ($nfn, "w"); Create a file
- Fwrite ($NF, $FF); Write file
- Fclose ($NF); Close File
- $i + +; Multi-file + +
- echo "";
- $content = Str_replace ($v, $NFN, $content);//Replace parameters in content
- }else{//the picture is not available, replace it with the default picture
- $content = Str_replace ($v, "/upload/201204/20120417213810742.gif", $content);//Replace parameters in content
- }
- }
- }
- }
Copy CodeExample 3,php a regular expression to download a picture locally.
/*
- Shortage: If the picture path in the page is not an absolute path, you cannot crawl
- */
- Set_time_limit (0);//crawl is not limited by time
$URL = ' http://pp.baidu.com/';//any URL
Get_pic ($URL);
function Get_pic ($pic _url) {
- Get Picture binary stream
- $data =curlget ($pic _url);
- /* Get a picture link using regular expressions */
- $pattern _src = '/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg])) [\ ' |\ '].*? [\/]?>/';
- $num = Preg_match_all ($pattern _src, $data, $match _src);
- Get an array of images $arr _src= $match _src[1];//
- Get_name ($arr _src);
echo " Finished!!! ";
- return 0;
- }
/* Get the picture type and save it to the same directory as the file */
- function get_name ($pic _arr)
- {
- Type of picture
- $pattern _type = '/(/. ( jpg|bmp|jpeg|gif|png))/';
foreach ($pic _arr as $pic _item) {//loops out the address of each picture
- $num = Preg_match_all ($pattern _type, $pic _item, $match _type);
- $pic _name = Get_unique (). Name of microsecond timestamp $match _type[1][0];//Change
- Save a picture as a stream
- $write _fd = @fopen ($pic _name, "WB");
- @fwrite ($write _fd, Curlget ($pic _item));
- @fclose ($write _fd);
- echo "[ok]..!";
- }
- return 0;
- }
Get a unique ID through microsecond time
- function Get_unique () {
- List ($msec, $sec) = Explode ("", Microtime ());
- Return $sec. Intval ($msec *1000000);
- }
Crawl Web content
- function Curlget ($url) {
- $url =str_replace (' & ', ' & ', $url);
- $curl = Curl_init ();
- curl_setopt ($curl, Curlopt_url, $url);
- curl_setopt ($curl, Curlopt_header, false);
curl_setopt ($curl, Curlopt_referer, $url);
- curl_setopt ($curl, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 6.0; seaport/1.2; Windows NT 5.1; SV1; infopath.2) ");
- curl_setopt ($curl, Curlopt_cookiejar, ' cookie.txt ');
- curl_setopt ($curl, Curlopt_cookiefile, ' cookie.txt ');
- curl_setopt ($curl, Curlopt_returntransfer, 1);
- curl_setopt ($curl, curlopt_followlocation, 0);
- $values = curl_exec ($curl);
- Curl_close ($curl);
- return $values;
- }
- ?>
Copy Code |