爬取百度搜索的图片
来源:互联网 发布:软件工程项目总监职责 编辑:程序博客网 时间:2024/05/22 14:17
#encoding:utf-8
import urllib
import requests #首先导入库
import re
MaxSearchPage = 20 # 收索页数
CurrentPage = 0 # 当前正在搜索的页数
DefaultPath = "/home/mindfusion/pic" # 默认储存位置
NeedSave = 0 # 是否需要储存
n=0
def imageFiler(content): # 通过正则获取当前页面的图片地址数组
return re.findall('"objURL":"(.*?)"',content,re.S)
def nextSource(content): # 通过正则获取下一页的网址
next = re.findall('<div id="page">.*<a href="(.*?)" class="n">',content,re.S)[0]
print("---------" + "http://image.baidu.com" + next)
return next
def spidler(source):
content = requests.get(source).text # 通过链接获取内容
imageArr = imageFiler(content) # 获取图片数组
global CurrentPage
print("Current page:" + str(CurrentPage) + "**********************************")
x = 0
for imageUrl in imageArr:
print(imageUrl)
global NeedSave
if NeedSave: # 如果需要保存保存
global DefaultPath
try:
picture = requests.get(imageUrl,timeout=10) # 下载图片并设置超时时间,如果图片地址错误就不继续等待了
except:
print("Download image error! errorUrl:" + imageUrl)
continue
urllib.urlretrieve(imageUrl,'%s.jpg' % x)
x=x+1
#pictureSavePath = DefaultPath + imageUrl.replace('/','') # 创建图片保存的路径
#fp = open(pictureSavePath,'wb') # 以写入二进制的方式打开文件 fp.write(picture.content)
#fp.close()
else:
global MaxSearchPage
if CurrentPage <= MaxSearchPage:
if nextSource(content):
CurrentPage += 1
spidler("http://image.baidu.com" + nextSource(content)) # 爬取完毕后通过下一页地址继续
def beginSearch(page=1,save=1,savePath="/home/mindfusion/pic"): # (page:爬取页数,save:是否储存,savePath:默认储存路径)
global MaxSearchPage,NeedSave,DefaultPath
MaxSearchPage = page
NeedSave = save
DefaultPath = savePath
key = "带口罩的人" #你要搜索的类别
StartSource = "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=" + str(key) + "&ct=201326592&v=flip" # 分析链接可以得到,替换其`word`值后面的数据来收索关键词
spidler(StartSource)
beginSearch(page=1,save=1)
import urllib
import requests #首先导入库
import re
MaxSearchPage = 20 # 收索页数
CurrentPage = 0 # 当前正在搜索的页数
DefaultPath = "/home/mindfusion/pic" # 默认储存位置
NeedSave = 0 # 是否需要储存
n=0
def imageFiler(content): # 通过正则获取当前页面的图片地址数组
return re.findall('"objURL":"(.*?)"',content,re.S)
def nextSource(content): # 通过正则获取下一页的网址
next = re.findall('<div id="page">.*<a href="(.*?)" class="n">',content,re.S)[0]
print("---------" + "http://image.baidu.com" + next)
return next
def spidler(source):
content = requests.get(source).text # 通过链接获取内容
imageArr = imageFiler(content) # 获取图片数组
global CurrentPage
print("Current page:" + str(CurrentPage) + "**********************************")
x = 0
for imageUrl in imageArr:
print(imageUrl)
global NeedSave
if NeedSave: # 如果需要保存保存
global DefaultPath
try:
picture = requests.get(imageUrl,timeout=10) # 下载图片并设置超时时间,如果图片地址错误就不继续等待了
except:
print("Download image error! errorUrl:" + imageUrl)
continue
urllib.urlretrieve(imageUrl,'%s.jpg' % x)
x=x+1
#pictureSavePath = DefaultPath + imageUrl.replace('/','') # 创建图片保存的路径
#fp = open(pictureSavePath,'wb') # 以写入二进制的方式打开文件 fp.write(picture.content)
#fp.close()
else:
global MaxSearchPage
if CurrentPage <= MaxSearchPage:
if nextSource(content):
CurrentPage += 1
spidler("http://image.baidu.com" + nextSource(content)) # 爬取完毕后通过下一页地址继续
def beginSearch(page=1,save=1,savePath="/home/mindfusion/pic"): # (page:爬取页数,save:是否储存,savePath:默认储存路径)
global MaxSearchPage,NeedSave,DefaultPath
MaxSearchPage = page
NeedSave = save
DefaultPath = savePath
key = "带口罩的人" #你要搜索的类别
StartSource = "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=" + str(key) + "&ct=201326592&v=flip" # 分析链接可以得到,替换其`word`值后面的数据来收索关键词
spidler(StartSource)
beginSearch(page=1,save=1)
0 0
- 爬取百度搜索的图片
- 爬取百度搜索结果的爬虫
- 从百度新闻爬取关键词搜索的页面
- Java爬取百度图片
- java爬取百度图片
- PHP爬取百度图片
- Python 爬取百度图片
- python爬取百度图片
- Python爬取百度图片
- python爬取百度图片
- Python 爬取百度图片
- Python爬取百度图片
- python爬取百度搜索结果
- 【爬虫】爬取百度搜索结果页面
- python爬取百度搜索动态网页
- 用requests爬取百度搜索数据
- XPath爬取百度搜索结果
- python爬虫爬取百度搜索结果
- Java关于Image和ImageIcon的小阴谋
- Linux ALSA声卡驱动之八:ASoC架构中的Platform
- 深入浅出Mybatis系列(十)---SQL执行流程分析(源码篇)
- 图的广度优先搜索(BFS)、深度优先搜索(DFS)(邻接矩阵法和邻接表法java实现)
- Chapter 6 Designing Classes
- 爬取百度搜索的图片
- python共享模式
- 作业2:个人名片
- 关于datetimepicker初始化的问题
- codeforces Igor and his way to work
- hashCode和equals方法
- 运用STL快速得到枚举排列
- opencv3.0官方文档
- SQL 重置表的id为1