爬虫案例--图片下载器

需求分析:
"我想要图片,我又不想上网搜“
“最好还能自动下载”
……

这就是需求,实现两个功能,一是搜索图片,二是自动下载

主要工作在分析网页和数据处理

爬虫案例--图片下载器_第1张图片

import requests
import os
import re

def req(url):
    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36'}
    try:
        response = requests.get(url,headers=headers)
    except Exception as e:
        print('获取失败',e)
    else:
        html = response.text
        downloadpic(html,headers)

def downloadpic(html,headers):
    pic_url = re.findall('"objURL":"(.*?)"',html,re.S)
    count=0
    for each in pic_url:
        try:
            pic=requests.get(each,headers=headers)
            print(pic)
        except requests.exceptions.ConnectionError as f:
            print('图片无法下载')
        except Exception as e:
            print(e)
        else:
            pic.raise_for_status()
            if not os.path.exists(savedir):
                os.makedirs(savedir)
            else:
                tail = each.split(".")[-1]
                if tail not in ['png', 'jpg', 'gif', 'jpeg']:
           			 continue
                count+=1
                filename=strpic+ str(count)+tail
                fullfilename = os.path.join(savedir,filename)
                with open(fullfilename,'wb') as f:
                    f.write(pic.content)

if __name__ == '__main__':
    strpic = input('please input str:')
    url = 'http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=' + strpic
    savedir='/home/kiosk/Desktop/pictures'
    req(url)

你可能感兴趣的:(python)