python爬虫实例:今日头条街拍大图下载

今天的目标是头条首页搜索街拍,下载各个标题下的大图,如下:
在这里插入图片描述

今天有点懒,仅分享下代码,自行学习消化:

import requests
import os
from hashlib import md5


for i in range(3):
    offset=i*20
    url='https://www.toutiao.com/api/search/content/?aid=24&app_name=web_search&offset=%d&format=json&keyword=街拍&autoload=true&count=20&en_qc=1&cur_tab=1'% offset
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}


    response=requests.get(url,headers=headers)
    #print(response.json())
    json=response.json()
    #print(json.get('data'))
    

    for titles in json.get('data'):
        try:
            print(titles['title'])
            print(titles['large_image_url'])
            if not os.path.exists(titles['title']):
                try:
                    os.mkdir(titles['title'])
                    image=requests.get(titles['large_image_url'])
                    if image.status_code == 200:
                        file_path='{0}/{1}.{2}'.format(titles['title'],md5(image.content).hexdigest(),'jpg')
                        #file_path = '{0}/{1}.{2}'.format(item.get('title'),md5(response.content).hexdigest(),'jpg')
                        if not os.path.exists(file_path):
                            with open(file_path,'wb') as o:
                                o.write(image.content) 
                    else:
                        print('')
                except OSError:
                    print('OSError')
        except KeyError:
            print('KeyError')

下载结果如下:
在这里插入图片描述
都是美女照片,宅男福利哦,赶紧动手吧。

感悟:心乱了,停下来整理下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值