阅读 343

PHP爬虫 -- 017 实战2 爬取猫眼电影top100

分析网站

需要爬取的数据

先建立数据表, 用来保存电影数据

CREATE TABLE `movies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `rank` varchar(255) NOT NULL,
  `score` varchar(255) NOT NULL,
  `actor` varchar(255) NOT NULL,
  `date` varchar(255) NOT NULL,
  `img` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
复制代码

使用IP代理池获取数据, 并打印输出

<?php

require 'vendor/autoload.php';
use QL\QueryList;
$base = "https://maoyan.com/board/4?offset=";

for ($i=0; $i < 10; $i++) { 
    $page = $i*10;
    $url = $base.$page;
    echo $url."\n";
    get_content($url);
}

function get_content($url){
    
    $data = QueryList::html(get_html_source($url))->rules([
        'title'=>['#app > div > div > div.main > dl > dd > div > div > div.movie-item-info > p.name > a','title'],
        'rank'=>['#app > div > div > div.main > dl > dd > i','text'],
        'img'=>['#app > div > div > div.main > dl > dd > a > img.board-img','data-src'],
        'actor'=>['#app > div > div > div.main > dl > dd > div > div > div.movie-item-info > p.star','text'],
        'date'=>['#app > div > div > div.main > dl > dd> div > div > div.movie-item-info > p.releasetime','text'],
        'score'=>['#app > div > div > div.main > dl > dd > div > div > div.movie-item-number.score-num > p','text'],
    ])->queryData();
    var_dump($data);
}
function get_html_source($url)
{
    // 要访问的目标页面
    $targetUrl = $url;
    // 代理服务器
    $proxyServer = "http://http-dyn.abuyun.com:9020";
    // 隧道身份信息
    $proxyUser = "H19D75L76VK89Q8D";
    $proxyPass = "8C17B0A80F475BD8";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $targetUrl);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // 设置代理服务器
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
    // 设置隧道验证信息
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    //$info = curl_getinfo($ch);
    curl_close($ch);
    return $result;
}
复制代码

把数据保存到数据库

<?php
require 'vendor/autoload.php';
use QL\QueryList;
use Medoo\Medoo;
// 创建数据库连接
$database = new medoo([
    'database_type' => 'mysql',
    'database_name' => 'demo_db',
    'server' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
]);



/* 
 * @Description: 从html页面中通过css选择器, 获取我们想要的数据
 * @param: 目标url
 * @return: 包含我们需要数据的二维数组
*/ 
function get_data($url) {
    echo "function get_data is running ... \n";
    $data = QueryList::html(get_html_source($url))->rules([
        "img" => ['#app > div > div > div.main > dl > dd > a > img.board-img', 'data-src'],
        "actor" => ['#app > div > div > div.main > dl > dd > div > div > div.movie-item-info > p.star', 'text'],
        "date" => ['#app > div > div > div.main > dl > dd > div > div > div.movie-item-info > p.releasetime', 'text'],
        "rank" => ['#app > div > div > div.main > dl > dd > i', 'text'],
        "score" => ['#app > div > div > div.main > dl > dd > div > div > div.movie-item-number.score-num > p', 'text'],
        "title" => ['#app > div > div > div.main > dl > dd > div > div > div.movie-item-info > p.name > a', 'text'],
    ])->queryData();
    return $data;
}

/* 
 * @Description: 保存数据到电影表, 传一个二维数组, 批量插入
 * @param: 二维数组
 * @return: 没有返回值
*/ 
function save_movie($data){
    echo "function save_movie is running ... \n";
    global $database;
    $database->insert('movies',$data);
}

/* 
 * @Description: 使用IP代理, 获取html代码
 * @param: 目标url
 * @return: html代码
*/ 
function get_html_source($url) {
    echo "function get_html_source is running ... \n";
    $result = false;
    while (!$result) {
        $targetUrl = $url;
        $proxyServer = "http://http-dyn.abuyun.com:9020";
        $proxyUser = "H19D75L76VK89Q8D";
        $proxyPass = "8C17B0A80F475BD8";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $targetUrl);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (!$result) {
            sleep(2);
        }
        curl_close($ch);
    }
    return $result;
}


// 基础的url, 为后面拼接具体的url做准备
$base_url = "https://maoyan.com/board/4?offset=";
// 只修改最后的数字, 拼接出来10条url
for ($i = 0; $i < 100; $i += 10) {
    $current_url = $base_url . $i;
    echo $current_url;
    $data = get_data($current_url);
    save_movie($data);
}
复制代码

提取数据并生成markdown文档

<?php
require 'vendor\autoload.php';
use Medoo\Medoo;
// 创建数据库连接
$database = new medoo([
    'database_type' => 'mysql',
    'database_name' => 'demo_db',
    'server' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
]);
/* 
 * @Description: 从数据库中取出电影的相关信息
 * @return: 二维数组
*/ 
function get_data(){
    global $database;
    $data = $database->select('movies','*');
    return $data;
}
/* 
 * @Description: 生成markdown文件
 * @param: 二维数组(电影信息)
 * @return: 没有返回值
*/ 
function make_markdown($data){
    $md_obj = fopen('maoyanmd.md','w+');
    foreach ($data as $key => $value) {
        $img = $value['img'];
        $img = str_replace("@160w_220h_1e_1c","@320w_440h_1e_1c",$img);
        $rank = $value['rank'];
        $title = $value['title'];
        $score = $value['score'];
        $date = $value['date'];
        $actor = $value['actor'];
        fwrite($md_obj,"![]({$img})\n");
        fwrite($md_obj,"## {$rank}-{$title}-{$score}\n");
        fwrite($md_obj,"> {$date}\n");
        fwrite($md_obj,"> {$actor}\n");
        fwrite($md_obj,"---\n\n\n");
    }
    fclose($md_obj);
}


make_markdown(get_data());
复制代码

作业

  • 爬取豆瓣电影top250, 把结果保存到数据库
  • 从数据库中提取电影信息, 做成markdown文件展示

关注下面的标签,发现更多相似文章
评论