美文网首页PHP Chaos
2018-03-01-简单到爆炸的 PHP 爬虫

2018-03-01-简单到爆炸的 PHP 爬虫

作者: xiaojianxu | 来源:发表于2018-03-01 17:38 被阅读12次

第一步:拉取链接内容

<?php
    // 数据采集
    /*
        https://www.hinabian.com/theme/detail/7187963787461753308.html
        https://www.hinabian.com/theme/detail/7161618734657271587.html
        // 7,161,618,734,657,271,587
    */
    header("Content-type: text/html; charset=utf-8");
    $ch = curl_init("http://www.51job.com/?from=baidupz");

    $fp = fopen("hinabian.txt", "w");
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    //curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

    $return = curl_exec($ch);
    curl_close($ch);
    fclose($fp);
?>

第二步:处理d拉取到的内容,使用正则表达式匹配出需要的内容,最后下载文件到本地。

<?php

    $source = fopen('hinabian.txt', 'r');
    $pattern = "/http:\/\/\S+(.png|jpg|jpeg|gif)/";

    $img = [];
    while ($row = fread($source, 1024)) {
        preg_match_all($pattern, $row, $match);
        $img[] = array_filter($match[0]);
    }

    $new_img = [];
    foreach($img as $item) {
        if ($item) {
            $new_img[] = $item;
        }
    }

    // 将多为数组转换为一维数组
    $new = [];
    function merge(array $parameter, &$new) {
        foreach($parameter as $item) {
            if (is_array($item) && $item) {
                merge($item, $new);
            }
            if (!is_array($item)) $new[] = $item;
        }
    }
    merge($new_img, $new);

    // 将图片文件下载到本地
    //header("content-type:application/x-msdownload");
    foreach($new as $item) {
        $fileName = explode('/', $item);
        $fileName = array_pop($fileName);
        $path = "download/$fileName";
        //header("content-disposition:attachement;filename={$fileName}");
        //readfile($path, false, fopen($item));//下载 
        // 读取远程连接文件,并保存到指定路径。
        $source = file_get_contents($item);
        file_put_contents($path, $source);
    }

相关文章

  • 2018-03-01-简单到爆炸的 PHP 爬虫

    第一步:拉取链接内容 第二步:处理d拉取到的内容,使用正则表达式匹配出需要的内容,最后下载文件到本地。

  • 各语言简单爬虫

    各语言简单爬虫 Python 简单爬虫 golang简单爬虫

  • 网络爬虫1--http协议和urllib

    爬虫初步 爬虫概念 都有哪些语言可以实现爬虫 ​ (1)php, 号称世界上最好的语言,可以实现爬虫,但做的不好...

  • 学渣讲爬虫之Python爬虫从入门到出门(第一讲)

    目录 学渣讲爬虫之Python爬虫从入门到出门第一讲 目录 爬虫的常用形式 爬虫的基本原理 前期准备 简单爬虫 爬...

  • PHP 爬虫

    昨天面试萌宝的php实习,给了我一个爬虫的题目,瞬间懵逼,我的天,从来没接触过爬虫啊。于是简单的了解了下,决定用h...

  • Goutte基本用法

    最近工作上用到PHP爬虫框架Goutte(号称是PHP上最好用的爬虫框架)。这里记下自己用到过的使用技巧,免得下次...

  • phpspider简单快速上手的php爬虫框架

    前言 前段时间接到一个开发采集网站数据的项目,从事php开发的我立刻想到使用php做爬虫。虽然python爬虫方便...

  • 浅谈爬虫

    1.什么是爬虫? 爬虫:就是抓取网页中的数据 2.为什么选择python做爬虫? 可以做爬虫的语言有很多,如PHP...

  • python-爬虫基础(慕课网)

    二.爬虫简介以及爬虫的技术价值 2-1:爬虫是什么? 2-2:爬虫技术的价值? 三.简单爬虫架构 3-1:简单爬虫...

  • php爬虫

    php爬虫可以用phpspider、querylisthttps://doc.phpspider.orghttp:...

网友评论

    本文标题:2018-03-01-简单到爆炸的 PHP 爬虫

    本文链接:https://www.haomeiwen.com/subject/vbknxftx.html