python爬虫之图片下载APP1.0

今天给大家来个好玩一点的,运用python爬取图片到本地,网站为https://www.pexels.com/
这个网站为外文网,所以搜索图片要用英语,今天要做的就是在python中进行搜索和下载图片,做一个网页版的APP。

直接上代码

from bs4 import BeautifulSoup
import requests

headers ={
    'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cookie':'__cfduid=dcb472bad94316522ad55151de6879acc1479632720; locale=en; _ga=GA1.2.1575445427.1479632759; _gat=1; _hjIncludedInSample=1',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}

url_path = 'https://www.pexels.com/search/'
content= input('请输入你要下载的图片:')
url = url_path + content + '/'
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
imgs = soup.select('a > img')
list = []
for img in imgs:
    photo = img.get('src')
    list.append(photo)

path = 'C://Users/Administrator/Desktop/photo/'

i = 1
for item in list:
    if item==None:
        pass
    elif '?' in item:
        data = requests.get(item,headers=headers)
        fp = open(path+content+str(i)+'.jpeg','wb')
        fp.write(data.content)
        fp.close
        i = i+1
    else:
        data = requests.get(item, headers=headers)
        fp = open(path+item[-10:],'wb')
        fp.write(data.content)
        fp.close()

分析代码

1我首先网站上分别搜索snow和girl,网站分别为:https://www.pexels.com/search/snow/
https://www.pexels.com/search/girl/
所以我利用input函数进行输入,然后自己构建url。
2解析和找到图片的url放到list中,这部分就不多讲了。
3之前用urlretrieve来下载一直报错,可能是外文网的原因,所以我把取到的图片的url再request一次,并加上了headers。
4为什么要用判断了?因为这个网站我爬取出现了None,我把它pass掉,其它有jpeg格式的,有png格式的,所以要分别下载。

    原文作者:python
    原文地址: https://www.jianshu.com/p/b8d0e58496fe
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞