python爬虫之图片下载APP1.0

作者: 罗罗攀 | 来源:发表于2016-12-22 15:55 被阅读426次

今天给大家来个好玩一点的,运用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格式的,所以要分别下载。

相关文章

网友评论

  • 4a1e55fb1dd8:你好,我爬取pexels图片也是和你这思路相同,但出现两个疑问:
    1.我在程序中获取img.get(‘src’)后,打印出来会把双引号也带进来,后来我把soup打印出来,发现解析后就是这样了: src='\"https://images.pexels.com/photos/87293/pexels-photo-87293.jpeg?h=350&auto=compress&cs=tinysrgb\"'
    2.用Beautifulsoup解析后的,原先属性title=“clean beauty water”中间是空格,就会解析为title =‘\"clean’,第一个空格后的就没了?
    希望能得到你的回复,谢谢!
    罗罗攀:@一如继往的蜗牛 这我也不知道啦
    4a1e55fb1dd8: @罗罗攀 谢谢,其实我想知道为什么会这样,之前解析其他网页没有出现过这种情况,是不是编码问题呢?
    罗罗攀:@一如继往的蜗牛 那就通过正则再匹配一下
  • 6bf4671977ce:网页版的app在哪
    罗罗攀:@噢瑞利 这只是说法,在我电脑里,就是一个小程序而已

本文标题:python爬虫之图片下载APP1.0

本文链接:https://www.haomeiwen.com/subject/lxqhvttx.html