How to crawl Web images in PHP

Source: Internet
Author: User
Tags foreach curl time limit

PHP is a very easy to get started Web programming language. PHP is a very low learning cost, can quickly develop a variety of Web applications, is a very good tool.

Although many people find php a lot of disadvantages, such as quick and dirty, "there are only two programming languages in the world, one is controversial and one is useless", isn't it? As long as the tools to do things well, is a good tool. PHP is such a good language tool.

01.<?php
02.header (' Content-type:text/html;charset=utf-8 ');
Set_time_limit (0);//Crawl not subject to time limit
if ($_post[' Submit ']== "Start crawling") {
$URL =$_post[' link '];
Get_pic ($URL);
07.}
function Get_pic ($pic _url) {
09.//Get Picture binary stream
$data =curlget ($pic _url);
11.//Using regular expressions to get picture links
$pattern _src1 = '//'//images only in JPG format
$pattern _src2 = '//'//images only in BMP format
$pattern _src3 = '//'//Picture only in PNG format
$pattern _SRC4 = '//'//Only matches a picture in GIF format
$num 1 = preg_match_all ($pattern _src1, $data, $match _src1);
$num 2 = Preg_match_all ($pattern _src2, $data, $match _src2);
$num 3 = Preg_match_all ($pattern _src3, $data, $match _src3);
$num 4 = Preg_match_all ($pattern _src4, $data, $match _SRC4);
$arr _src1= $match _src1[1];//get an array of pictures
$arr _src2= $match _src2[1];
$arr _src3= $match _src3[1];
$arr _src4= $match _src4[1];
Echo ' ============================================= crawl start =============================================<br/ > ';
Get_name1 ($arr _src1);
Get_name1 ($arr _src2);
Get_name1 ($arr _src3);
Get_name1 ($arr _SRC4);
Get_name2 ($arr _src1);
Get_name2 ($arr _src2);
Get_name2 ($arr _src3);
Get_name2 ($arr _SRC4);
33.
Echo ' ============================================= crawl end =============================================<br/ > ';
return 0;
36.}
37.
function get_name1 ($pic _arr) {
39.//Picture number and type
$pattern _type = '/.*\/(. *?) $/';
41.
foreach ($pic _arr as $pic _item) {//loop out the address of each picture
$num = Preg_match_all ($pattern _type, $pic _item, $match _type);
44.//Save Picture as Stream
$write _fd = @fopen ($match _type[1][0], "WB");
Image URL: <a href= ' ". $pic _item." ' target= ' _blank ' > '. $pic _item. " </a><br/> ";
@fwrite ($write _fd, Curlget ($pic _item));
@fclose ($write _fd);
49.}
return 0;
51.}
function get_name2 ($pic _arr) {
53.//Picture number and type
$pattern _type = '/.*\/(. *?) $/';
55.
foreach ($pic _arr as $pic _item) {//loop out the address of each picture
$num = Preg_match_all ($pattern _type, $pic _item, $match _type);
58.//Save Picture as Stream
$write _fd = @fopen ($match _type[1][0], "WB");
echo "Image URL: <a href= '". $_post[' link '. $pic _item. "' target= ' _blank ' >". $_post[' link ']. $pic _item. " </a><br/> ";
@fwrite ($write _fd, curlget ($_post[' link '). $pic _item));
@fclose ($write _fd);
63.}
return 0;
65.}
66.//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);
72.
//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);
Bayi. return $values;
82.}
83.?>
84.<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Web Image Capture </title>
<body>
<form action= "" method= "POST" >
91. To crawl the image of the URL: <input type= "text" id= "link" name= "link" value= "Please enter here to capture the image URL" onclick= "this.value=" "" size= "100"/& Gt;<br/>
<input type= "Submit" id= "submit" name= "Submit" value= "Start crawling"/>
</form>
</body>
95.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.