前言

运行下面的代码会随机得到妹子图的一张图片,代码中的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不然会报错。

利用 PhpQuery 随机爬取妹子图的更多相关文章

  1. Python 爬虫入门(二)——爬取妹子图

    Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...

  2. Python 爬虫入门之爬取妹子图

    Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...

  3. scrapy 也能爬取妹子图?

    目录 前言 Media Pipeline 启用Media Pipeline 使用 ImgPipeline 抓取妹子图 瞎比比前言 我们在抓取数据的过程中,除了要抓取文本数据之外,当然也会有抓取图片的需 ...

  4. 使用request+Beautiful爬取妹子图

    一.request安装 pip install requests request使用示例 import requests response = requests.get('https://www.mz ...

  5. requests+正则表达式 爬取 妹子图

    做了一个爬取妹子图某张索引页面的爬虫,主要用request和正则表达式. 感谢 崔庆才大神的 爬虫教学视频 和 gitbook: B站:https://www.bilibili.com/video/a ...

  6. 爬取妹子图(requests + BeautifulSoup)

    刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为: ...

  7. 小白学 Python 爬虫(16):urllib 实战之爬取妹子图

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  8. python 爬取妹子图

    作为一个python还没入门的小白,搞懂这段代码实在是很不容易,还要去学html的知识(#黑脸) 因此我加上了注释,比较好读懂点 #coding=utf-8 import time import re ...

  9. python实战项目 — 爬取 妹子图网,保存图片到本地

    重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...

随机推荐

  1. 配置centOS下的Python

    Linux下Python版本升级: 1. 首先确认Linux操作系统中自带的python 版本时候与自己所需要的版本一致 所有的python版本都在https://www.python.org/ftp ...

  2. Java 学习笔记 第一章:Java语言开发环境搭建

    第一章:Java语言开发环境搭建 第二章:常量.变量和数据类型 第三章:数据类型转换.运算符和方法入门 1.Java虚拟机——JVM JVM(Java Virtual Machine ):Java虚拟 ...

  3. Debug 是门艺术

    最近想结合发生在身边码农身上的一些小故事,尝试表达一个观点“Coding 是门技术,Debug 是门艺术”. 上期的分享<Coding 是门技术>主要通过引入身边 Code farmer ...

  4. Step by Step!教你如何在k3s集群上使用Traefik 2.x

    本文来自边缘计算k3s社区 作者简介 Cello Spring,瑞士人.从电子起步,拥有电子工程学位.尔后开始关注计算机领域,在软件开发领域拥有多年的工作经验. Traefik是一个十分可靠的云原生动 ...

  5. stm32:简单按键输入实现

    开发环境keil4,芯片STM32F103C8T6 1.main.c //串口实验 #include "sys.h" #include "delay.h" #i ...

  6. python实现杨辉三角形

    代码实现: # python实现杨辉三角形 def yanghui(): # 定义第一行列表为[1] line = [1] while True: # yield的作用:把一个函数变成生成器,同时返回 ...

  7. java 根据图片文字动态生成图片

    今天在做热敏打印机打印二维码,并有文字描述,想到的简单的方法就是根据热敏打印机的纸张宽度和高度,生成对应的图片,如下: package com.orisdom.utils; import lombok ...

  8. 个推IGt.BaseTemplate.php,不仅有bug,还有bom头,好恶心!

    错误截图,提交吧,还有一个不明飞行物. 去掉utf-8 BOM:set nobomb保留utf-8 BOM:set bomb

  9. python-nmap 使用基础

    前言 python-nmap是一个Python库,可帮助您使用nmap端口扫描程序.它可以轻松操纵nmap扫描结果,将是一个完美的选择想要自动执行扫描任务的系统管理员的工具和报告. 它还支持nmap脚 ...

  10. php--phpstudy更新数据库版本后,无法一键启动

    只需输入以下命令即可: sc delete mysql