Maison > php教程 > php手册 > PHP简单网页图片抓取类

PHP简单网页图片抓取类

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-06 19:35:25
original
1340 Les gens l'ont consulté

使用例子 ?php include imgCatcher.class.php; header(content-type:text/html;charset=utf-8); $catcher = New imgCatcher(http://www.oschina.net/,images,jpg); $catcher-get_pic(); ? 无 ?php class imgCatcher{const FLODER_DIR = 'image/';const IMG_TY

使用例子
  
  include "imgCatcher.class.php";
  header('content-type:text/html;charset=utf-8');
  $catcher = New imgCatcher("http://www.oschina.net/","images","jpg");

  $catcher->get_pic();

?>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

<?php

    class imgCatcher{

 

        const FLODER_DIR = 'image/';

        const IMG_TYPE = 'jpg|bmp|png|gif';

 

        var $floder;

        var $url;

        var $type;

        var $domain;

        var $pic_arr;

 

        public function __construct($url,$floder='',$type=''){

            $this->url = $url;

             

            $url_arr = parse_url($url);

 

            $this->domain = array_shift($url_arr);

             

            if(trim($floder) == ""){

                $this->folder = FLODER_DIR;

            }else{

                $this->folder = $floder.'/';

            }

 

            if(trim($type)==""){

                $this->type = IMG_TYPE;

            }else{

                $this->type = $type;

            }

             

            $this->pic_arr = array();

            if(!is_dir(__DIR__.'/'.$this->folder))

                mkdir(__DIR__.'/'.$this->folder);

        }

 

        public function get_pic(){

            set_time_limit(0);//抓取不受时间限制 

            //获取图片二进制流 

            $data = self::CurlGet($this->url); 

            //利用正则表达式得到图片链接 

            $pattern_src = '/<img .*?src\=\"(.*\.('.$this- alt="PHP简单网页图片抓取类" >type.')).*?>/';//

            $num = preg_match_all($pattern_src, $data, $match_src); 

 

            $this->pic_arr=$match_src[1];//获得图片数组

 

 

            $this->get_name();  

 

            return 0; 

        }

 

        public function get_name(){ 

            $pic_arr = $this->pic_arr;

             

            //图片编号和类型 

            $pattern_type = '/.*\/(.*?)$/'

             

            foreach($pic_arr as $pic_item){//循环取出每幅图的地址 

             

                $num = preg_match_all($pattern_type,$pic_item,$match_type); 

                //以流的形式保存图片 

                $write_fd = fopen($this->folder.$match_type[1][0],"wb"); 

 

                fwrite($write_fd, self::CurlGet($pic_item,$this->url)); 

                fclose($write_fd); 

            }

 

            return 0; 

        }

 

 

         //抓取网页内容 

        static function CurlGet($url,$domain=""){

            if(substr($url, 0, 1) == "/"){

                $url $domain.$url;

            }

 

            if(substr($url, 0, 1) == "."){

                $url $domain.substr($url, 1);

            }

 

            if(substr($url, 0, 2) == ".."){

                $url $domain.substr($url, 2);

            }          

            $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

        }    

 

    }

 

 

 ?>

Copier après la connexion
Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal