美文网首页Mac使用技巧Mac优雅使用指南
营造一个好的编码心情-python抓取mac精美壁纸

营造一个好的编码心情-python抓取mac精美壁纸

作者: 简书说我的昵称违规 | 来源:发表于2017-11-23 14:42 被阅读55次

如图,一个好的工作环境,可以让心情好不少

image.png

抓取的是爱壁纸的资源,它们最多只提供20页一个类别,但是一页有60张。
总共有11个类别,就是有20x60x11张。我这里只筛选了2种类别,看你需要了。

话不多说,直接上代码吧

# coding=utf-8
from pymongo import MongoClient
import requests
import logging
import uniout
import json
import logger
import threading
import time
import sys
import os




# 创建 日志 对象
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
    '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# mongodb
mongoconn = MongoClient('127.0.0.1', 27017)
mdb = mongoconn.data_analysis
das_collection = mdb.bizhi

#
categories = {
    "moviestar": 1,
    "landscape": 2,
    "beauty": 3,
    "plant": 4,
    "animal": 5,
    "game": 6,
    "cartoon": 7,
    "festival": 8,
    "car": 798,
    "food": 1546,
    "sport": 1554
};

# pic path
pic_path = '/Library/Desktop Pictures/'


def scrapy_it(page, tid, width=2560, height=1600):
    # 地址
    start_url = '''http://api.lovebizhi.com/macos_v4.php?a=category&tid=%d&
    device=105&uuid=436e4ddc389027ba3aef863a27f6e6f9&mode=0&retina=1&
    client_id=1008&device_id=31547324&model_id=105&size_id=0&channel_id=
    70001&screen_width=%d&screen_height=%d&version_code=19&order=newest&color_id=3&p=%d''' % (
        tid, width, height, page)
    print start_url
    res = requests.get(start_url)
    content = res.json()
    return content


def getFilename(url):
    url_split = url.split('/')
    name = url_split[len(url_split) - 1]
    name_split = name.split(',')
    ext_split = name.split('.')
    ext = ext_split[1]
    return name_split[0] + '.' + ext


def store_it(follow):
    if das_collection.find_one({'id': follow['id']}):
        logging.debug(u'%s在库中已存在' % follow['id'])
    else:
        print type(follow['id'])
        logging.debug('插入记录:%s' % follow['id'])
        das_collection.insert_one(follow)


def download_it(link, filename):
    if os.path.exists(filename):
        logging.info(u'图片%s已存在' % filename)
        return False
    res = requests.get(link)
    with open(filename, 'wb') as f:
        f.write(res.content)
    logging.info(u'存入图片%s' % filename)


if __name__ == '__main__':

    tids = [categories["landscape"], categories['plant']]
    for tid in tids:
        for p in range(1, 21):
            res = scrapy_it(p, tid)
            data_list = res['data']
            for data in data_list:
                if 'vip_original' in data['image']:
                    img_url = data['image']['vip_original']
                else:
                    img_url = data['image']['original']
                filename = pic_path + getFilename(img_url)
                download_it(img_url, filename)

相关文章

  • 营造一个好的编码心情-python抓取mac精美壁纸

    如图,一个好的工作环境,可以让心情好不少 抓取的是爱壁纸的资源,它们最多只提供20页一个类别,但是一页有60张。总...

  • 用Mac os自带Automator抓取网页自动存储txt文件

    最近都在自学python,看到用python可以抓取网页内容,于是想到了mac os 自带的Automator可视...

  • python抓取简单爬虫01

    python抓取简单爬虫时,如果抓取的网页爬虫数据里面,有不能够utf-8编码的(例如html里面的简体中文), ...

  • 2019-08-12 Python3 'utf-8' codec

    注意Excel转Tab-txt时,文本的编码方式可能不同 最近编写了一个Python脚本用于从网页抓取特定的数据,...

  • python编码(3.21学习内容)

    使用场景 1、读取一个文件时,文件保存使用的编码格式决定了我们从文件读取内容时使用的格式 2、从python抓取网...

  • 好文推荐

    python GIL全局解释器锁的理解 爬虫实战项目合集 python的字符编码问题 计算机的ip地址与MAC地址...

  • python编码

    python编码 python编码简介 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,...

  • Python实用练手小案例

    抓取网页信息,并生成txt文件内容!Python抓取网页技能——Python抓取网页就是我们常看见的网络爬虫,我们...

  • python 爬虫之路 之Scrapy框架

    Scrapy 介绍 Scrapy,Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点...

  • requests编码

    Python requests在作为代理爬虫节点抓取不同字符集网址的时候会遇到一些编码的问题,通过查找资料将自己理...

网友评论

    本文标题:营造一个好的编码心情-python抓取mac精美壁纸

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