从豆瓣爬取并下载对应关键字的全部图片(以周杰伦为例)

源码

import requests
import json

def download(src, id):
    dir = './' + str(id) + '.jpg'
    try:
        pic = requests.get(src, timeout=10)
    except requests.exceptions.ConnectionError:
        # print 'error, %d 当前图片无法下载', %id
        print('图片无法下载')
    fp = open(dir, 'wb')
    fp.write(pic.content)
    fp.close()

def loop_request(total, text):
    for i in range(0, total, 20):
        url = 'https://www.douban.com/j/search_photo?q=' + text + '&limit=20&start=' + str(i)
        html = requests.get(url).text  # 得到返回结果
        response = json.loads(html, encoding='utf-8')  # 将JSON格式转换成Python对象
        for image in response['images']:
            print('downloading ' + image['src'])
            # 查看当前下载的图片网址
            download(image['src'], image['id'])  # 下载一张图片

def get_count(text):
    url = 'https://www.douban.com/j/search_photo?q=' + text + '&limit=20&start=1'
    html = json.loads(requests.get(url).text, encoding='utf-8')
    print(html)
    print(html['total'])
    loop_request(html['total'], text)

get_count('周杰伦')

简要说明

  • 其中包含三个函数
  • get_count用于获取该关键字对应图片的数量
  • loop_request根据图片总数量,20个一组获取图片
  • download下载图片并根据id保存至本地

环境介绍

  • python版本 3.0+
  • 开发工具 PyCharm

END

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章