• Python 爬虫爬取今日头条街拍上的图片


    # 今日头条--街拍
    
    import requests
    from urllib.parse import urlencode
    import os
    from hashlib import md5
    from multiprocessing.pool import Pool
    
    # 根据 offset 得到每一个 ajax 请求返回的 json
    def get_json(offset):
        base_url = 'https://www.toutiao.com/search_content/?'
        params = {
            'offset': offset,
            'format': 'json',
            'keyword': '街拍',
            'autoload': 'true',
            'count': '20',
            'cur_tab': '1',
            'from': 'search_tab',
            'pd': 'synthesis'
        }
        url = base_url + urlencode(params)
    
        try:
            response = requests.get(url)
            if response.status_code == 200:
                return response.json()
        except requests.ConnectionError as e:
            print('Errors', e.args)
            return None
    
    # 根据 josn 提取出相应内容的标题、图片链接
    def get_images(json):
        if json.get('data'):
            for item in json.get('data'):
                if item.get('cell_type') is not None:
                    continue
                title = item.get('title')
                images = item.get('image_list')
                for image in images:
                    yield {
                        'title': title,
                        #获取大图片
                        'image': image.get('url').replace('list', 'large').replace('//', 'Https://')
                    }
    
    # 根据 item 中的 title 创建文件夹,图片的名称可以用其内容的 MD5 值,防止重复
    # 这里有个小问题,那就是在 windows 路径下创建文件夹是不可以有英文的':',这些标题大多是中文符号,但是偶尔也会含有
    # 英文的':',这样会导致创建文件夹失败,所以要将windows下不允许的英文符号(/:*?"<>|)转换成相应的中文标点
    def save_images(item):
        title = item.get('title')
        intab = r'/:*?"<>|'
        outtab = '、、:-?“《》-'
        trantab = str.maketrans(intab, outtab)
        # 将windows下不允许的英文符号(/:*?"<>|)转换成相应的中文标点
        title = title.translate(trantab)
        if not os.path.exists(title):
            os.mkdir(title)
        try:
            response = requests.get(item.get('image'))
            if response.status_code == 200:
                file_path = '{0}/{1}.{2}'.format(title, md5(response.content).hexdigest(), 'jpg')
                if not os.path.exists(file_path):
                    with open(file_path, 'wb') as f:
                        f.write(response.content)
                else:
                    print('Already Downloaded', file_path)
        except requests.ConnectionError:
            print('Failed to save image')
    
    def main(offset):
        json = get_json(offset)
        for item in get_images(json):
            print(item)
            save_images(item)
    
    # 定义开始页数
    GROUP_START = 1
    # 定义结束页数
    GROUP_END = 5
    
    if __name__ == '__main__':
        pool = Pool()
        offsets = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])
        # 利用多线程的线程池的map方法下载图片
        pool.map(main, offsets)
        pool.close()
        pool.join()
    
  • 相关阅读:
    基于ObjectCache的应用
    数学趣题——验证角谷猜想
    数学趣题——递归法寻找最小值
    数学趣题——寻找同构数
    数学趣题——表示成两个数的平方和
    数学趣题——马克思手稿中的数学题
    数学趣题——具有特殊性质的数
    数学趣题——验证四方定理
    数学趣题——连续整数固定和问题
    数学趣题——验证尼克彻斯定理
  • 原文地址:https://www.cnblogs.com/yunche/p/10244473.html
Copyright © 2020-2023  润新知