PHP爬虫百度图片

时间:2023-02-23 16:35:21

php pacong_1.php "酒","电报","电表","电波","电厂","电场","电车","电池","电传","电磁",
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
$word2=explode(",",$argv[1]);
$pageNum = 35;
$uuid = md5(uniqid(mt_rand(), true));
//获取数据
foreach($word2 as $k=>$v) {
$encodeWord = urlencode(yang_gbk2utf8($v));
for ($i=1; $i < $pageNum; $i++) {
$page = ($i)*30;
//百度头像图片获取api
$url = "http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&ie=utf-8&oe=utf-8&word=".$encodeWord."&pn=".$page."&rn=30&gsm=700001e&1457697756442=";
echo $url."\r\n";
$imgJsonData = file_get_contents($url);
//随机采样,让每次获取的图片尽量不是相同的,可以增加获取图片api去减少获取重复的图片的几率
$imgJsonData = json_decode($imgJsonData,true);
//print_r($imgJsonData);
foreach ($imgJsonData['data'] as $key => $dataArr) {
if ($dataArr['thumbURL']) {
downloadImg($dataArr['thumbURL'],$uuid.$k);
}
}
}
}

function downloadImg($url,$k) {
$path = './userSysAvatorUrl/'.$k."/";
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$headers=array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection: keep-alive",
"Host: img0.imgtn.bdimg.com",
"Referer: http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%CD%B7%CF%F1&fr=ala&oriquery=%E5%A4%B4%E5%83%8F&ala=1&alatpl=portait&pos=0",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0",
"X-Requested-With: XMLHttpRequest"
);
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//执行并获取HTML文档内容
$imgBin = curl_exec($ch);
curl_close($ch);
list($msec, $sec) = explode(' ', microtime());
$file_name = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
$shullf = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$rand_key = array_rand($shullf,3);
$file_name = $shullf[$rand_key[0]].$shullf[$rand_key[1]].$shullf[$rand_key[2]].'_'.$file_name. '.jpg';
file_put_contents($path.$file_name, $imgBin);
//释放curl句柄

}

function yang_gbk2utf8($str){
$charset = mb_detect_encoding($str,array('UTF-8','GBK','GB2312'));
$charset = strtolower($charset);
if('cp936' == $charset){
$charset='GBK';
}
if("utf-8" != $charset){
$str = iconv($charset,"UTF-8//IGNORE",$str);
}
return $str;
}

?>

PHP爬虫百度图片的更多相关文章

  1. 百度图片爬虫-python版-如何爬取百度图片&quest;

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  2. 【Python网络爬虫四】通过关键字爬取多张百度图片的图片

    最近看了女神的新剧<逃避虽然可耻但有用>,同样男主也是一名程序员,所以很有共鸣 被大只萝莉萌的一脸一脸的,我们来爬一爬女神的皂片. 百度搜索结果:新恒结衣 本文主要分为4个部分: 1.下载 ...

  3. Python爬虫:通过关键字爬取百度图片

    使用工具:Python2.7 点我下载 scrapy框架 sublime text3 一.搭建python(Windows版本) 1.安装python2.7 ---然后在cmd当中输入python,界 ...

  4. python爬虫之爬取百度图片

    ##author:wuhao##爬取指定页码的图片,如果需要爬取某一类的所有图片,整体框架不变,但需要另作分析#import urllib.requestimport urllib.parseimpo ...

  5. 如何用Python爬虫实现百度图片自动下载?

    Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...

  6. python爬虫获取百度图片(没有精华,只为娱乐)

    python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...

  7. python3 TensorFlow训练数据集准备 下载一些百度图片 入门级爬虫示例

    从百度图片下载一些图片当做训练集,好久没写爬虫,生疏了.没有任何反爬,随便抓. 网页: 动态加载,往下划会出现更多的图片,一次大概30个.先找到保存每一张图片的json,其对应的url: 打开调试,清 ...

  8. python爬虫-爬取百度图片

    python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...

  9. Python 爬虫实例(1)—— 爬取百度图片

    爬取百度图片  在Python 2.7上运行 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: loveNight import jso ...

随机推荐

  1. dos命令

    一 .常用命令 1 dir无参数:查看当前所在目录的文件和文件夹./s:查看当前目录已经其所有子目录的文件和文件夹./a:查看包括隐含文件的所有文件./ah:只显示出隐含文件./w:以紧凑方式(一行显 ...

  2. js常用字符串处理方法

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  3. XML格式示例 与 XML操作&lpar;读取&rpar;类封装

    header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...

  4. &lbrack;转&rsqb; Linux中启动和停止jar包的运行

    脚本一: startTest.sh内容如下:#!/bin/shjava -jar Test.jar &       #注意:必须有&让其后台执行,否则没有pid生成echo $! &g ...

  5. C&plus;&plus; 遇到的问题小结

    1. cannot convert 'std::basic_string<char>' to 'int' in assignment ... 原始code如下: int id2; std: ...

  6. 从Windows 服务器通过sync向Linux服务器定时同步文件

    本文解决的是Windows 下目录及文件向Linux同步的问题,Windows向 Windows同步的请参考:http://www.idcfree.com/article-852-1.html 环境介 ...

  7. Esper系列&lpar;九&rpar;NamedWindow语法create、Insert、select

    功能:用于存储一种或多种类型的事件的集合,并能对所存储的事件进行增删改查操作. CreateNameWindow 根据已有的数据源构造 格式: 1  [context context_name]  2 ...

  8. 一个PDO类

    下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...

  9. openNebula dubug

    lowest common denominator convenient way to manage virtual machines

  10. 第四章Android移植环境搭建

    第四章Android移植环境搭建 这一章主要学习如何搭建 Android 移植的环境.因为 Android 底层是基于 Linux 内核的,所以本章从交叉编译环境等嵌入式开发环境的搭建开始,介绍了 B ...