利用phpQuery随机爬取美图录图片

jkzyx 2020-04-21

前言

运行下面的代码会随机得到妹子图的一张图片,代码中的phpQuery可以在这里下载:phpQuery-0.9.5.386.zip

<?php
 
require ‘phpQuery.php‘;
 
// 主体域名
$basicUrl = ‘https://www.meitulu.com/‘;
 
// 分类名称
$category = array(‘nvshen‘, ‘jipin‘, ‘nenmo‘, ‘wangluohongren‘, ‘fengsuniang‘, ‘qizhi‘, ‘youwu‘,
    ‘baoru‘, ‘xinggan‘, ‘youhuo‘, ‘meixiong‘, ‘shaofu‘, ‘changtui‘, ‘mengmeizi‘,
    ‘loli‘, ‘keai‘, ‘huwai‘, ‘bijini‘, ‘qingchun‘, ‘weimei‘, ‘qingxin‘);
 
// 爬虫代码
function curl($url, $referer, $download)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4‘));
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, -1);
    $contents = curl_exec($ch);
    curl_close($ch);
    if ($download) {
        $resource = fopen(‘default.jpg‘, ‘w‘);
        fwrite($resource, $contents);
        fclose($resource);
        return;
    }
    return $contents;
}
 
$count = 10;
 
// 随机分类
while ($count > 0) {
    $afterUrl = $basicUrl . ‘t/‘ . $category[rand(0, count($category) - 1)] . ‘/‘ . rand(2, 5) . ‘.html‘;
    $html = curl($afterUrl, $afterUrl, false);
    if (strlen($html) != 0) {
        break;
    }
    $count--;
}
 
if($count == 0){
    echo ‘爬取失败!‘;
    exit;
}
 
$count = 10;
 
$afterUrlTmp = $afterUrl;
$eg = phpQuery::newDocument($html);
$links = pq(‘ul.img > li > a‘);
 
// 随机套图
$afterUrl = ‘‘;
for ($i = 0; $i < count($links); $i++) {
    $afterUrl = $links->eq($i)->attr(‘href‘);
    if (strpos($afterUrl, ‘item‘ !== false)) {
        if (strpos($afterUrl, ‘https‘ == false)) {
            $afterUrl = ‘https://www.meitulu.com‘ + $afterUrl;
        }
        $html = curl($afterUrl, $afterUrlTmp, false);
        if (strlen($html) != 0) {
            break;
        }
    }
}
 
$html = curl($afterUrl, $afterUrlTmp, false);
$eg = phpQuery::newDocument($html);
$img = pq(‘img.content_img‘);
 
$afterUrlTmp = $afterUrl;
 
// 随机图片
while ($count > 0) {
    $afterUrl = $img->eq(rand(0, count($img) - 1))->attr(‘src‘);
    if (strlen($afterUrl) != 0) {
        break;
    }
    $count--;
}
 
if($count == 0){
    echo ‘爬取失败!‘;
    exit;
}
 
curl($afterUrl, $afterUrlTmp, true);
echo ‘<img src="default.jpg">‘;
 
?>

场景

至于该代码的使用场景就不用我多说了吧,放在博客的侧边栏或者随便一个什么地方都可以,这里说明一下php版本最好是5.x.x不然会报错。

相关推荐