爬取百度搜索的图片
2017-04-24 12:08
225 查看
#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)
相关文章推荐
- 百度日文图片搜索面经
- 如何才能在百度搜索结果中出现图片
- 百度公布图片搜索收录展现的基本要求
- selenium结合sikuli实现百度的图片搜索功能
- 百度CSND博客在搜索栏中显示图片
- 【百度爬虫系列 I】多关键字图片搜索结果汇总
- Baidu:C#利用百度来搜索网页、图片、视频等等
- 百度Ueditor编辑器取消多图上传对话框中的图片搜索
- Python根据关键字百度搜索下载图片
- python百度搜索url爬取 图片
- 百度CSND博客在搜索栏显示图片
- 怎么让自己的网站在百度搜索中带图片显示
- 百度搜索结果升级 显示嵌入图片
- 删除百度Ueditor编辑器 在线管理 图片搜索 功能
- [置顶] Python3实现批量下载百度搜索图片
- Python爬虫抓取百度搜索图片
- 主要搜索引擎(Google和百度、雅虎)的站内搜索代码
- 百度撤销企业软件事业部 称与搜索主方向背离
- 百度和Google谁更能搜索色情信息