爬虫应用——抓取百度图片

完整代码

import requests             # 首先导入库
import re

# 设置默认设置
MaxSearchPage = 20             # 搜索页面
CurrentPage = 0                # 当前正在搜索的页数
DefaultPath = "pictures"       # 默认储存位置
NeedSave = 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) + "************")
    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
            # 创建图片保存的路径
            imageUrl = imageUrl.replace('/', ' ').replace(':', ' ').replace('?', ' ')
            pictureSavePath = DefaultPath + imageUrl
            fp = open(pictureSavePath, 'wb')          # 以写入二进制的方式打开文件
            fp.write(picture.content)
            fp.close()
    global MaxSearchPage
    if CurrentPage <= MaxSearchPage:
        if nextSource(content):
            CurrentPage += 1
            # 爬取完毕后通过下一页地址继续爬取
            spidler("http://image.baidu.com" + nextSource(content))

# 爬虫的开启方法
def beginSearch(page = 1, save = 0, savePath = "E:/img/"):
    # (page:爬取页数,save:是否储存,savePath:默认储存路径)
    global MaxSearchPage, NeedSave, DefaultPath
    MaxSearchPage = page
    NeedSave =save                  # 是否保存,值为0不保存,1保存
    DefaultPath = savePath          # 图片保存的位置
    key = input("Please input what you want to search:")
    StartSource = "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=" + str(key) + "&ct=201326592&cv=flip"
    # 分析链接可以得到,替换其“word”值后面的数据来搜索关键词
    spidler(StartSource)

# 调用开启的方法就可以通过关键词搜索图片了
beginSearch(page = 5, save = 1)     # page=5是下载前5页,save=1为保存图片

结果显示

TIM截图20191119091854.png
TIM截图20191119091923.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容