# -*- coding:utf-8 -*-
#https://blog.csdn.net/qq_32166627/article/details/60882964
import requests
import os
import pinyin
import simplejson def getManyPages(keyword,pages):
params=[]
for i in range(30,30*pages+30,30):
params.append({
'tn': 'resultjson_com',
'ipn': 'rj',
'ct': 201326592,
'is': '',
'fp': 'result',
'queryWord': keyword,
'cl': 2,
'lm': -1,
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': -1,
'z': '',
'ic': 0,
'word': keyword,
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': 0,
'istype': 2,
'qc': '',
'nc': 1,
'fr': '',
'pn': i,
'rn': 30,
'gsm': '1e',
'': ''
})
url = 'https://image.baidu.com/search/acjson'
urls = []
for i in params:
#print("begin")
try:
rgjson = requests.get(url,params=i).json().get('data')
except simplejson.scanner.JSONDecodeError:
print('【错误】simplejson.scanner.JSONDecodeError ')
continue
#print("end")
urls.append(rgjson) return urls def getImg(dataList, localPath, keyword): if not os.path.exists(localPath): # 新建文件夹
os.mkdir(localPath) x = 0
for list in dataList:
for i in list:
if i.get('thumbURL') != None:
#print('download:%s' % i.get('thumbURL'))
print("down " + str(x) + " image " + i.get('thumbURL'))
ir = requests.get(i.get('thumbURL'))
open(localPath +"/" + keyword + '_%d.jpg' % x, 'wb').write(ir.content)
x += 1
else:
print('image not exist') def convert():
fp = open("stars_list_clean.txt",'w')
with open("stars_list.txt",'r') as face_file:
stars_list = face_file.readlines()
index = 0
line_record = []
for line in stars_list:
line = line.replace('\r','').replace('\n','').replace('\t','')
#print(line)
line_split = line.strip().split(",")
print(line_split[1])
if line_split[1] not in line_record:
line_record.append(line_split[1])
fp.write('%s\n' % line_split[1])
else:
print(line_split[1], " is exist") def debug(): # with open("stars_list_clean.txt",'r') as face_file:
# stars_list = face_file.readlines()
# index = 0
# for line in stars_list:
# line = line.replace('\r','').replace('\n','').replace('\t','')
# keyword_english = pinyin.get(line, format="strip")
# keyword = line
# index += 1
# if index > 0:
# break # print(keyword)
# keyword1 = '胡因梦'
# if keyword == keyword1:
# print("yes")
# else:
# print("no")
keyword = '胡因梦'
keyword_english = "hym"
dataList = getManyPages(keyword,2) # 参数1:关键字,参数2:要下载的页数
getImg(dataList,'./hanxue', keyword_english) # 参数2:指定保存的路径 # keyword = '韩雪'
# dataList = getManyPages(keyword,2) # 参数1:关键字,参数2:要下载的页数
#getImg(dataList,'./hanxue') # 参数2:指定保存的路径 def run(): fp = open("stars_list_en.txt",'w')
with open("stars_list_clean.txt",'r') as face_file:
stars_list = face_file.readlines()
for line in stars_list:
line = line.replace('\r','').replace('\n','').replace('\t','')
keyword_english = pinyin.get(line, format="strip")
fp.write('%s\n' % keyword_english)
face_ID_index = 0 dir = "./stars_srcimg/" # if os.path.exists(dir):
# os.system("rm -rf " + dir) if not os.path.exists(dir):
os.mkdir(dir) pages = 5
maxnum = pages * 30
print(maxnum) for line in stars_list:
#line.decode('utf-8').encode('gb2312')
line = line.replace('\r','').replace('\n','').replace('\t','')
keyword = line
print keyword
keyword_english = pinyin.get(keyword, format="strip")
print keyword_english
face_ID = str(face_ID_index) + "_" + keyword
facesavepath = dir + str(face_ID_index) + "_" + keyword
face_ID_index += 1
print facesavepath
if not os.path.exists(facesavepath):
os.mkdir(facesavepath)
else:
print(keyword, " exist")
continue print("down " + keyword) dataList = getManyPages(keyword, pages) # 参数1:关键字,参数2:要下载的页数
getImg(dataList, facesavepath, face_ID) # 参数2:指定保存的路径 if __name__ == '__main__':
debug()
#run()

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

  1. python写的百度图片爬虫

    学了一下python正则表达式,写一个百度图片爬虫玩玩. 当技术遇上心术不正的人,就成我这样的2B青年了. python3.6开发.程序已经打包好,下载地址: http://pan.baidu.com ...

  2. 百度图片爬虫-python版-如何爬取百度图片?

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

  3. 百度图片爬虫-python版

               self.browser=imitate_browser.BrowserBase()            self.chance=0            self.chanc ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. linux关闭selinux和firewall

    sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config setenforce 0

  2. 关于CAE的那点儿破事儿

    CAE是计算机辅助工程的英文简写,所涵盖的范围甚是广泛.现在很多人提到CAE,总是联想到结构有限元计算,更有甚者认为有限元就是CAE.还有人把所有的工程数值计算都称作有限元.本文就这一话题,来谈谈关于 ...

  3. openssl生成SSL证书的流程

    SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socketlayer(SSL),SSL安全协议主要用来提供对用户和服务器的认证:对传送的数据进行加密和隐藏:确保数 ...

  4. [SQL Server 2014] SQL Server 2014新特性探秘

    SQL Server 2014新特性探秘(1)-内存数据库   简介 SQL Server 2014提供了众多激动人心的新功能,但其中我想最让人期待的特性之一就要算内存数据库了.去年我再西雅图参加SQ ...

  5. 重复代码检查工具simian的基本用法

    simian是一个检查重复代码的工具,支持通过命令行和UI方式来检查代码,可以检查多种语言(比如C\C++, java, c#等)的代码,常见的编程语言都支持,下面先来看看如何使用命令行来检查c++重 ...

  6. 每日英语:China's Bigger Innovation Problem

    Last month's Third Plenum meeting of Chinese leaders seemed to signal Beijing's intention to experim ...

  7. python类里面使用装饰器

    参考:http://m.blog.csdn.net/blog/wklken/8118942 def on(name, *args_of_on, **kwargs_of_on): def handle_ ...

  8. 使用canvas制作简单表格

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Python(六)之文件对象

    Python文件对象 明确文件系统: 获取文件对象: var_name = open(file_name[mode,[bufsize]]) 缓冲: 0:禁用 1:使用缓冲,只缓冲一行数据 2+:指定缓 ...

  10. idea 换主题

    换背景 . 选中行变色