python写的百度图片爬虫
学了一下python正则表达式,写一个百度图片爬虫玩玩。
当技术遇上心术不正的人,就成我这样的2B青年了。
python3.6开发。程序已经打包好,下载地址: http://pan.baidu.com/s/1bpalugf 密码:kfk4
#!/usr/local/env python
from tkinter import *
import re,os,requests,hashlib,threading
from PIL import Image class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.school=threading.local()
self.pack()
self.createWidgets() def createWidgets(self):
self.nameLabel=Label(self,text='请输入关键词:')
self.nameLabel.grid(row=0,sticky=W) self.nameInput = Entry(self)
self.nameInput.grid(row=0,column=1) self.picys=IntVar()
self.Checkbutton = Checkbutton(self,text='图片压缩',variable=self.picys)
self.Checkbutton.grid(row=1,column=0,columnspan=2,sticky=W) self.alertButton = Button(self, text='下载',command=self.gorun)
self.alertButton.grid(row=1,column=1,sticky=E) def cddir(self):
keyword=self.nameInput.get()
os.chdir('C:\\Users\\Administrator\\Desktop\\')
if os.path.exists(keyword) ==False:
os.mkdir(keyword)
os.chdir(keyword) def gorun(self):
self.cddir()
word=self.nameInput.get()
x=0
for i in range(5):
t=threading.Thread(target=self.xiazai,args=(x,word,))
t.start()
x+=20
if i == 4:
t.join()
self.delfile()
if self.picys.get() == 1:
self.suoxiao()
def xiazai(self,page,word):
baidupn=self.school.student=page
num=1
for i in range(50):
url='https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8'
payload={'word':word,'pn':baidupn}
html = requests.get(url,params=payload).text
regular='"objURL":"(.*?)",'
pic=re.findall(regular,html)
baidupn+=100 for tu in pic:
try:
dl=requests.get(tu,timeout=60)
pic_name=os.path.basename(tu)
if pic_name in os.walk(os.getcwd()):
continue
else:
if ('?' in pic_name) or ('&' in pic_name) or ('.' not in pic_name):
pic_name='%s%s' %(num,'.jpg')
with open(pic_name,"wb") as code:
code.write(dl.content)
requests.session().keep_alive = False
dl.close()
num+=1
except requests.exceptions.ConnectionError:
#print('这张图片下载失败了,图片地址',tu)
continue def suoxiao(self):
self.cddir()
filedir=os.walk(os.getcwd())
for i in filedir:
for tplb in i[2]:
if ('jpg' in tplb) or ('jpeg' in tplb):
try:
im=Image.open(tplb)
w,h=im.size
if w > 500:
im.thumbnail((w//2,h//2))
im.save(tplb,'jpeg')
im.close()
except OSError:
print('跳过此文件') def md5sum(self,filename):
f=open(filename, 'rb')
md5=hashlib.md5()
while True:
fb = f.read(8096)
if not fb:
break
md5.update(fb)
f.close()
return (md5.hexdigest()) def delfile(self):
all_md5={}
self.cddir()
filedir=os.walk(os.getcwd())
for i in filedir:
for tlie in i[2]:
if self.md5sum(tlie) in all_md5.values():
os.remove(tlie)
else:
all_md5[tlie]=self.md5sum(tlie) app=Application()
app.master.title('图片下载器')
app.mainloop()
无耻的求一下赞助
python写的百度图片爬虫的更多相关文章
- 百度图片爬虫-python版-如何爬取百度图片?
上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...
- 用python写一个百度翻译
运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...
- 用 Python 批量下载百度图片
为了做一个图像分类的小项目,需要制作自己的数据集.要想制作数据集,就得从网上下载大量的图片,再统一处理. 这时,一张张的保存下载,就显得很繁琐.那么,有没有一种方法可以把搜索到的图片直接下载到本地 ...
- 百度图片爬虫-python版
self.browser=imitate_browser.BrowserBase() self.chance=0 self.chanc ...
- python 百度图片爬虫
# -*- coding:utf-8 -*- #https://blog.csdn.net/qq_32166627/article/details/60882964 import requests i ...
- Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)
这道题是一道爬虫练习题,需要爬链接http://tieba.baidu.com/p/2166231880里的所有妹子图片,点进链接看一下,这位妹子是日本著名性感女演员--杉本由美,^_^好漂亮啊,赶紧 ...
- python写的百度贴吧相册下载
突然想搞个这样的工具,写来写去都不知道在干嘛了,本来两个文件,现在整合在一起了. 乱得不行,懒得整理了,能用就行. 下载部分用了多线程,但是下载一个文件还是用的单线程,也就是没管http头的range ...
- 【python小练】图片爬虫之BeautifulSoup4
Python3用不了Scrapy! Python3用不了Scrapy! Python3用不了Scrapy! [重要的事情说三遍,据说大神们还在尝试把scrapy移植到python3,特么浪费我半个小时 ...
- python爬取百度图片
import requests import re from urllib import parse import os from threading import Thread def downlo ...
随机推荐
- SparkSQL学习笔记
概述 冠状病毒来临,宅在家中给国家做贡献之际,写一篇随笔记录SparkSQL的学习笔记,目的有二,一是记录整理之前的知识作为备忘录,二是分享技术,大家共同进步,有问题也希望大家不吝赐教.总体而言,大数 ...
- Spring循环依赖原理
Spring循环依赖的原理解析 1.什么是循环依赖? 我们使用Spring的时候,在一个对象中注入另一个对象,但是另外的一个对象中也包含该对象.如图: 在Student中包含了teacher的一个 ...
- 【解决了一个小问题】alert manager中的cluster.advertise-address参数是什么意思?
如果在启动 alert manager的时候,不填写参数: /usr/bin/alertmanager --config.file=/etc/alert_manager/alertmanager.ya ...
- 「DP 浅析」斜率优化
#0.0 屑在前面 将结合经典例题 「HNOI2008」玩具装箱 以及 「NOI2007」货币兑换 进行讲解. #1.0 简述 #1.1 适用情况 斜率优化一般适用于状态转移方程如下的 DP \[f_ ...
- golang中int、float、string数据类型之间的转换
package main import ( "fmt" "strconv" ) func main() { var num1 int = 88 var num2 ...
- Visual Studio 2022 下载链接及激活密钥
Visual Studio 2022 下载链接:https://visualstudio.microsoft.com/zh-hans/vs/ 激活码: 专业版: TD244-P4NB7-YQ6XK-Y ...
- Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources failed.
说明 今天发现这个错误,然后整体检查了一下代码,没有发现任何错误,最后没法只有来一步一步排查. 解决 确定pom文件是否有问题 如上图,有红色波浪线,代表错误,请检查并解决,还有版本是否冲突,最好把不 ...
- Nginx实现跨域配置详解
主要给大家介绍了关于Nginx跨域使用字体文件的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 问题描述 今天在使用子域名访问根域名的CSS时,发现字体无法显示,在确保CSS和Font字 ...
- IDE添加自定义注释
前言:最近在找IDE自定义模板注释时,十分不愉快,找了很久,才找到适合自己的,故记录一下 一.IDE自定义类注释: 1:打开自定义模板界面,并添加自定义内容: 2:新建类,效果如下 备注: ...
- git init和git init –bare的区别:
感谢原文作者:ljchlx 原文链接:https://blog.csdn.net/ljchlx/article/details/21805231 git init 和 git init –bare 的 ...