Python百度图片爬虫
视频演示搜索B站“速光网络”
import requests # 首先导入库
import re
import os
# 设置默认配置
MaxSearchPage = 20 # 收索页数
CurrentPage = 1 # 当前正在搜索的页数
DefaultPath = "./百度图片" # 默认储存位置
NeedSave = 0 # 是否需要储存
# 图片链接正则和下一页的链接正则
def imageFiler(content): # 通过正则获取当前页面的图片地址数组
return re.findall('"objURL":"(.*?)"', content, re.S) # 获取.*?中URL
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):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
content = requests.get(source, headers=headers).text # 通过链接获取内容
imageArr = imageFiler(content) # 获取图片数组
global CurrentPage
print("Current page:" + str(CurrentPage) +
"**********************************")
for imageUrl in imageArr:
print(imageUrl)
global NeedSave
if NeedSave: # 如果需要保存图片则下载图片,否则不下载图片
global DefaultPath
try:
# 下载图片并设置超时时间,如果图片地址错误就不继续等待了
# picture为返回的图片二进制数据
picture = requests.get(imageUrl, timeout=10).content
except:
print("Download image error! errorUrl:" + imageUrl)
continue
# 创建图片保存的路径
imageUrl = imageUrl.replace(
'/', '').replace(':', '').replace('?', '') # 将'/',':','?'替换为' '
pictureSavePath = DefaultPath+'/'+imageUrl+'.jpg'
if not os.path.exists(DefaultPath):
os.mkdir(DefaultPath)
try:
with open(pictureSavePath, 'wb') as fp:
fp.write(picture)
except:
continue
global MaxSearchPage
if CurrentPage < MaxSearchPage: # 继续下一页爬取
if nextSource(content):
CurrentPage += 1
# 爬取完毕后通过下一页地址继续爬取
spidler("http://image.baidu.com" + nextSource(content))
# 爬虫的开启方法
def beginSearch(page=1, save=0, savePath=".//百度图片"):
# (page:爬取页数,save:是否储存,savePath:默认储存路径)
global MaxSearchPage, NeedSave, DefaultPath
MaxSearchPage = page
NeedSave = save # 是否保存,值0不保存,1保存
DefaultPath = savePath # 图片保存的位置
key = input("请输入搜索关键词:")
StartSource = "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=" + \
str(key) + "&ct=201326592&v=flip" # 分析链接可以得到,替换其`word`值后面的数据来搜索关键词
spidler(StartSource)
# 调用开启的方法就可以通过关键词搜索图片了
beginSearch(page=30, save=1) # page=30是下载前30页,save=1保存图片
完美源码 » Python百度图片爬虫