利用 PhpQuery 随机爬取妹子图
前言
运行下面的代码会随机得到妹子图的一张图片,代码中的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 随机爬取妹子图的更多相关文章
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- Python 爬虫入门之爬取妹子图
Python 爬虫入门之爬取妹子图 来源:李英杰 链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...
- scrapy 也能爬取妹子图?
目录 前言 Media Pipeline 启用Media Pipeline 使用 ImgPipeline 抓取妹子图 瞎比比前言 我们在抓取数据的过程中,除了要抓取文本数据之外,当然也会有抓取图片的需 ...
- 使用request+Beautiful爬取妹子图
一.request安装 pip install requests request使用示例 import requests response = requests.get('https://www.mz ...
- requests+正则表达式 爬取 妹子图
做了一个爬取妹子图某张索引页面的爬虫,主要用request和正则表达式. 感谢 崔庆才大神的 爬虫教学视频 和 gitbook: B站:https://www.bilibili.com/video/a ...
- 爬取妹子图(requests + BeautifulSoup)
刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为: ...
- 小白学 Python 爬虫(16):urllib 实战之爬取妹子图
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- python 爬取妹子图
作为一个python还没入门的小白,搞懂这段代码实在是很不容易,还要去学html的知识(#黑脸) 因此我加上了注释,比较好读懂点 #coding=utf-8 import time import re ...
- python实战项目 — 爬取 妹子图网,保存图片到本地
重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...
随机推荐
- poj——1182食物链 并查集(提升版)
因为是中文题,题意就不说了,直接说思路: 我们不知道给的说法中的动物属于A B C哪一类,所以我们可以用不同区间的数字表示这几类动物,这并不影响结果,我们可以用并查集把属于一类的动物放在一块,举个例子 ...
- 12c OCR corrupted results in CRS stack down.
12c OCR corrupted results in CRS stack down. 1. check crsd.trc2017-03-21 16:14:44.667838 : CRSOCR:2 ...
- [HDU2546]饭卡<dp 01背包>
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 #题目描述: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前, ...
- CodeForces 506B/505D Mr. Kitayuta's Technology
Portal:http://codeforces.com/problemset/problem/506/B http://codeforces.com/problemset/problem/505/D ...
- Web 环境设置
修改最大打开文件数量 ulimit -n 100000 修改创建文件的最大值 #/etc/security/limits.conf * soft nofile 262140 * hard nofile ...
- M - 湫湫系列故事——减肥记I
M - 湫湫系列故事--减肥记I 对于吃货来说,过年最幸福的事就是吃了,没有之一! 但是对于女生来说,卡路里(热量)是天敌啊! 资深美女湫湫深谙"胖来如山倒,胖去如抽丝"的道理,所 ...
- 六、【Docker笔记】Docker数据管理
前几节我们介绍了Docker的基本使用和三大核心概念,那么我们在使用Docker的过程中,Docker中必然产生了大量的数据,对于这些数据我们需要查看或者对这些数据进行一个备份,也有可能容器之间的数据 ...
- JavaScript RegExp.$1...$9 属性详解
RegExp.$1...$9属性用于返回正则表达式模式中某个子表达式匹配的文本. 正则表达式中每个小括号内的部分表达式就是一个子表达式. 该属性是RegExp全局对象的一个只读属性,所有主流浏览器均支 ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- 标准与扩展ACL
标准与扩展ACL 案例1:配置标准ACL 案例2:配置扩展ACL 案例3:配置标准命名ACL 配置扩展命名ACL 1 案例1:配置标准ACL 1.1 问题 络调通后,保证网络是通畅的.同时也很可能出现 ...