php中如何抓取网页图片2014-08-31PHP是一门很容易上手的Web编程语言。PHP学习成本很低,能够迅速开发各种Web应用,是一个很优秀的工具。尽管很多人觉得PHP缺点很多,quick and dirty 之类的,但是“这个世界上只有两种编程语言,一种是饱受争议的,一种是没人用的”,不是吗?只要能够把事情做好的工具,就是好工具。PHP就是这么一个优秀的语言工具。
01.<?php
02.header("content-type:text/html;charset=utf-8");
03. set_time_limit(0);//抓取不受时间限制
04. if($_POST["Submit"]=="开始抓取"){
05.$URL=$_POST["link"];
06.get_pic($URL);
07. }
08. function get_pic($pic_url) {
09.//获取图片二进制流
10.$data=CurlGet($pic_url);
11.//利用正则表达式得到图片链接
12.$pattern_src1 = "/<img.*?src="(.*.jpg).*?>/";//只匹配jpg格式的图片
13.$pattern_src2 = "/<img.*?src="(.*.bmp).*?>/";//只匹配bmp格式的图片
14.$pattern_src3 = "/<img.*?src="(.*.png).*?>/";//只匹配png格式的图片
15.$pattern_src4 = "/<img.*?src="(.*.gif).*?>/";//只匹配gif格式的图片
16.$num1 = preg_match_all($pattern_src1, $data, $match_src1);
17.$num2 = preg_match_all($pattern_src2, $data, $match_src2);
18.$num3 = preg_match_all($pattern_src3, $data, $match_src3);
19.$num4 = preg_match_all($pattern_src4, $data, $match_src4);
20.$arr_src1=$match_src1[1];//获得图片数组
21.$arr_src2=$match_src2[1];
22.$arr_src3=$match_src3[1];
23.$arr_src4=$match_src4[1];
24.echo "=============================================抓取开始=============================================<br />";
25.get_name1($arr_src1);
26.get_name1($arr_src2);
27.get_name1($arr_src3);
28.get_name1($arr_src4);
29.get_name2($arr_src1);
30.get_name2($arr_src2);
31.get_name2($arr_src3);
32.get_name2($arr_src4);
33.
34.echo "=============================================抓取结束=============================================<br />";
35.return 0;
36. }
37.
38. function get_name1($pic_arr){
39.//图片编号和类型
40.$pattern_type = "/.*/(.*?)$/";
41.
42.foreach($pic_arr as $pic_item){//循环取出每幅图的地址
43. $num = preg_match_all($pattern_type,$pic_item,$match_type);
44. //以流的形式保存图片
45. $write_fd = @fopen($match_type[1][0],"wb");
46. echo "图片网址:<a href="".$pic_item."" target="_blank">".$pic_item."</a><br />";
47. @fwrite($write_fd, CurlGet($pic_item));
48. @fclose($write_fd);
49.}
50.return 0;
51. }
52.function get_name2($pic_arr){
53.//图片编号和类型
54.$pattern_type = "/.*/(.*?)$/";
55.
56.foreach($pic_arr as $pic_item){//循环取出每幅图的地址
57. $num = preg_match_all($pattern_type,$pic_item,$match_type);
58. //以流的形式保存图片
59. $write_fd = @fopen($match_type[1][0],"wb");
60. echo "图片网址:<a href="".$_POST["link"].$pic_item."" target="_blank">".$_POST["link"].$pic_item."</a><br />";
61. @fwrite($write_fd, CurlGet($_POST["link"].$pic_item));
62. @fclose($write_fd);
63.}
64.return 0;
65. }
66. //抓取网页内容
67. function CurlGet($url){
68.$url=str_replace("&","&",$url);
69.$curl = curl_init();
70.curl_setopt($curl, CURLOPT_URL, $url);
71.curl_setopt($curl, CURLOPT_HEADER, false);
72.
73.//curl_setopt($curl, CURLOPT_REFERER,$url);
74.curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
75.curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
76.curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt");
77.curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
78.curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
79.$values = curl_exec($curl);
80.curl_close($curl);
81.return $values;
82. }
83.?>
84.<html>
85. <head>
86. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
87.<title>网页图片抓取</title>
88. </head>
89. <body>
90.<form action="" method="post">
91. 要抓取图片的网址:<input type="text" id="link" name="link" value="请在这里输入要抓取图片的网址" OnClick="this.value=""" size="100" /><br />
92. <input type="submit" id="Submit" name="Submit" value="开始抓取" />
93.</form>
94. </body>
95.</html>