python 百度图片爬虫
# -*- 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 百度图片爬虫的更多相关文章
- python写的百度图片爬虫
学了一下python正则表达式,写一个百度图片爬虫玩玩. 当技术遇上心术不正的人,就成我这样的2B青年了. python3.6开发.程序已经打包好,下载地址: http://pan.baidu.com ...
- 百度图片爬虫-python版-如何爬取百度图片?
上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...
- 百度图片爬虫-python版
self.browser=imitate_browser.BrowserBase() self.chance=0 self.chanc ...
- 【Python网络爬虫四】通过关键字爬取多张百度图片的图片
最近看了女神的新剧<逃避虽然可耻但有用>,同样男主也是一名程序员,所以很有共鸣 被大只萝莉萌的一脸一脸的,我们来爬一爬女神的皂片. 百度搜索结果:新恒结衣 本文主要分为4个部分: 1.下载 ...
- Python爬虫:通过关键字爬取百度图片
使用工具:Python2.7 点我下载 scrapy框架 sublime text3 一.搭建python(Windows版本) 1.安装python2.7 ---然后在cmd当中输入python,界 ...
- 如何用Python爬虫实现百度图片自动下载?
Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...
- python爬虫获取百度图片(没有精华,只为娱乐)
python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
- python爬虫之爬取百度图片
##author:wuhao##爬取指定页码的图片,如果需要爬取某一类的所有图片,整体框架不变,但需要另作分析#import urllib.requestimport urllib.parseimpo ...
随机推荐
- [svc]jdk+tomcat部署.jforum论坛部署
安装jdk和tomcat jdk1.7.0_13(系列)下载url 我这里用的最新的jdk. 去官网下载即可 cd /usr/local/src/ tar xf jdk-8u162-linux-x64 ...
- MVC的项目部署成应用程序或虚拟目录路径的问题
1.js和css的引用出错 a.~/可以取得应用程序目录 b. ./定位到路径,./代表到本目录,../代表父级目录 2.打开页面view a. ./定位到路径 3.img src a. ./定位 ...
- 每日英语:American Cities May Have Hit 'Peak Office'
Despite some hype and a few regional exceptions, the construction of office towers and suburban offi ...
- GitHub支持的Markdown语法 GitHub Flavored Markdown
GitHub支持的Markdown语法,简称GFM.相比标准的Markdown(SM)语法,有少数几个区别,并添加了新的功能. 本位参考 GitHub Flavored Markdown 撰写.有兴趣 ...
- 有了#ifdef 为什么还需要#if defined
有了#ifdef 为什么还需要#if defined ? #include <stdio.h> #define A #define B void test(int a,int b) { ...
- 固定高度div,随内容自动变高css定义方法
*{ font-size:12px; margin:0; padding:0;}方法1:#testBox{border:1px solid #cccccc;padding:5px;width:220p ...
- iOS开发中的火星坐标系及各种坐标系转换算法
原文地址:http://m.oschina.net/blog/619183?ref=myread 其原理是这样的:保密局开发了一个系统,能将实际的坐标转换成虚拟的坐标.所有在中国销售的数字地图必须使用 ...
- asp.net基于StateServer的二级域名共享session
备注:亲自试验有效,如果网友有通过下面的教程未实现session共享的,欢迎留言说明你遇到的问题.必有回复. 最近为实现的二级域名共享session纠结好久.网上的很多实现的方法试了都不行,查了很久才 ...
- 开源一个爬取redmine数据的测试报告系统
背景 软件测试的最后有一道比较繁琐的工作,就是编写测试报告.手写测试报告在数据统计和分析上面要耗费比较大的事件和精力.之前工作室使用mantis管理bug缺陷.公司有内部有个系统,可以直接从manti ...
- jQuery 实现前端模糊匹配与首字母搜索
实现效果 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...