Python实战计划学习笔记:week4 总结

![Uploading Screen Shot 2016-07-27 at 16.46.58_522860.png . . .]
](http://upload-images.jianshu.io/upload_images/1504853-805d329ac707cd16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

代码部分
网页顶栏部分:

    <div class="ui fixed menu" style="height:54px;padding:10px 0px 10px 10px">
      <div class="ui right labeled input">
        <div class="ui basic label" style="padding:2px 0 2px 5px"><i class="red big pinterest icon"> </i></div>
        <input type="text" placeholder="Search..." style="background-color:#dfdfdf;min-width:356px">
        <div class="ui basic label"><i class="sidebar icon"></i></div>
      </div>

      <div class="ui left attached basic button" style="margin:0 0 0 10px">
        <i class="red pin icon"></i>YOUR ACCOUNT
      </div>
      <div class="right attached ui basic button" >
        <i class="right setting icon"></i>
      </div>
    </div>

卡片图片部分

<div class="ui link cards" style="margin:60px 0 0 0px" id='contentor'>
<div class="ui card" style="width:236px">
        <div class="image">
          <img src="{% static 'images/pinpic01.jpg' %}">
        </div>
        <div class="content">
          <div class="header">Matt Giampietro</div>
          <div class="meta">
            <a>Friends</a>
          </div>
          <div class="description">
            Matthew is an interior designer living in New York.
          </div>
        </div>
        <div class="extra content">
          <span class="right floated">
            Joined in 2013
          </span>
          <span>
            <i class="user icon"></i>
            75 Friends
          </span>
        </div>
 </div>
</div>

二手行情网站:

首先在ipython中进行数据清洗整理,再通过pipeline实现:

发帖总量.png
one day.png
一天的cates和areas.png

之后移植到Django中。

views.py

from django.shortcuts import render
from ganji.models import ItemInfo
from django.core.paginator import Paginator

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 不同区域发帖量前5名
def Topx(date1,date2,area,limit):
    pipeline=[
        {'$match':{'$and':[{'pub_date':{'$gte':date1,'$lte':date2}},{'area':{'$all':area}}]}},
        {'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
        {'$limit':limit},
        {'$sort':{'counts':-1}}
    ]

    for i in ItemInfo._get_collection().aggregate(pipeline):
        data = {
            'name': i['_id'][0],
            'data': [i['counts']],
            'type': 'column'
        }
        yield data

series_CY = [i for i in Topx('2016.01.01','2016.01.07',['朝阳'],5)]
series_TZ = [i for i in Topx('2016.01.01','2016.01.07',['通州'],5)]
series_HD = [i for i in Topx('2016.01.01','2016.01.07',['海淀'],5)]

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 数据中发帖总量柱状图
def total_post():
    pipeline = [
    {'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
    ]

    for i in ItemInfo._get_collection().aggregate(pipeline):
        data = {
            'name':i['_id'][0],
            'y':i['counts']
        }
        yield data

series_post=[i for i in total_post()]

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def one_day_deal_cate():
    pipeline = [
        {'$match':{'$and':[{'pub_date':{'$gte':'2015.12.25','$lte':'2016.01.11'}},{'time':1}]}},
        {'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
        {'$sort':{'counts':1}}
    ]
    for i in ItemInfo._get_collection().aggregate(pipeline):
        data = {
            'name':i['_id'][0],
            'y':i['counts']
        }
        yield data

def one_day_deal_area():
    pipeline = [
        {'$match':{'$and':[{'pub_date':{'$gte':'2015.12.25','$lte':'2016.01.11'}},{'time':1}]}},
        {'$group':{'_id':{'$slice':['$area',1]},'counts':{'$sum':1}}},
        {'$sort':{'counts':1}}
    ]

    for i in ItemInfo._get_collection().aggregate(pipeline):
        data = {
            'name':i['_id'][0],
            'y':i['counts']
        }
        yield data

pie1_data = [i for i in one_day_deal_cate()]
pie2_data = [i for i in one_day_deal_area()]

#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


def index(request):
    context = {
        'chart_CY':series_CY,
        'chart_TZ':series_TZ,
        'chart_HD':series_HD,
        'series_post':series_post,
        'pie1_data':pie1_data,
        'pie2_data':pie2_data
    }
    return render(request,'chart2.html',context)

def web(request):
    return render(request,'web.html')

def pin(request):
    return render(request,'pin.html')

# blog
def chart(request):
    limit = 15
    item_info = ItemInfo.objects[:20]
    pageinator = Paginator(item_info, limit)
    page = request.GET.get('page', 1)
    print(request)
    print(request.GET)

    loaded = pageinator.page(page)

    context = {
        'ItemInfo': loaded,
        'counts': item_info.count(),
        'last_time':item_info.order_by('-pub_date').limit(1),
    }
    return render(request,'index_data.html',context)

models.py

from django.db import models

from mongoengine import *

# Create your models here.
class ItemInfo(Document):
    title = StringField()
    url = StringField()
    pub_date = StringField()
    area = ListField(StringField())
    cates = ListField(StringField())
    look = StringField()
    time = StringField()
    price = IntField()
    meta = {'collection':'item_info'}

urls.py

from django.conf.urls import url
from django.contrib import admin
from ganji.views import index,web,pin,chart

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', index),
    url(r'^web/',web),
    url(r'^pin/',pin),
    url(r'^chart/',chart)

]

index_data.html

{% extends "chart.html" %}

{% block grid %}
<div class="ui equal width grid" style="width:70%;margin:5px 0 5px 0;">
            <div class="row">
                <div class="column">
                    <div class="ui red segment">
                        <div class="ui statistic">
                            <div class="value">
                                {{ counts }}
                            </div>
                            <div class="label">
                                Documents
                            </div>
                        </div>
                    </div>
                </div>
                <div class="column">
                    <div class="ui blue segment">
                        <div class="ui statistic">
                            <div class="value">
                                <i class="plane icon"></i> 5
                            </div>
                            <div class="label">
                                Flights
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="column">
                    <div class="ui container segment">
                        <div class="ui divided items">
                            {% for item in ItemInfo %}
                            <div class="item">
                                <div class="content">
                                    <div class="header">
                                        {{ item.title }}
                                    </div>
                                    <div class="description">
                                        {{ item.area }}
                                    </div>
                                    <div class="extra">
                                        {% for tag in item.cates %}
                                        <div class="ui label">
                                            {{ tag }}
                                        </div>
                                        {% endfor %}
                                    </div>

                                </div>
                            </div>
                            {% endfor %}
                        </div>

                        <div class = "ui small pagination menu">
                            {% if ItemInfo.has_previous %}
                                <a class="icon item" href="?page={{ ItemInfo.previous_page_number }}">
                                <i class="icon left arrow"></i>
                                </a>
                            {% endif %}

                            <div class="disabled item">
                                {{ ItemInfo.number }} of {{ ItemInfo.paginator.num_pages }}
                            </div>
                            {% if ItemInfo.has_next %}
                                <a class="icon item" href="?page={{ ItemInfo.next_page_number }}">
                                    <i class="icon right arrow"></i>
                                </a>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
{% endblock %}

chart2.html

{% extends 'chart.html' %}

{% block grid %}
<div class="ui equal width grid" style="margin:5px 0 5px 0;width:70%">


    <div class="row">
      <div class="column">
        <div class="ui container segment">
          <div class="ui container" id="chart2"></div>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="column">
        <div class="ui container segment">
          <div class="ui container" id="pie1"></div>
        </div>
      </div>

      <div class="column">
          <div class="ui container segment">
          <div class="ui container" id="pie2"></div>
        </div>
      </div>
    </div>


    <div class="row">
      <div class="column">
        <div class="ui container segment">
          <div class="ui compact menu">
            <div class="ui simple dropdown item">
              Area
              <i class="dropdown icon"></i>
              <div class="menu">
                <div class="item" id="CY">朝阳</div>
                <div class="item" id="HD">海淀</div>
                <div class="item" id="TZ">通州</div>
              </div>
            </div>
          </div>
          <div class="ui container" id="chart1"></div>
        </div>
      </div>
    </div>



</div>

{% endblock %}

{% block chartjs %}

    <script>
    $(function () {
        // Create the chart
        $('#chart2').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: '发帖总量柱状图'
            },
            xAxis: {
                type: 'category'
            },
            yAxis: {
                title: {
                    text: '数量'
                }
            },
            legend: {
                enabled: false
            },

            series: [{
                name: 'posts',
                colorByPoint: true,
                data: {{ series_post|safe }}
            }]
        });
    });

    </script>

    <script>
        $('#CY').click(function () {
          $('#chart1').highcharts({
            credits:{
                enabled:false
            },
            title: {
                text: '朝阳二手交易'
            },
            yAxis: {
                title: {
                    text: 'TOP 5'
                }
            },
            series: {{ chart_CY|safe }}
        });
    });
    </script>

    <script>
        $('#HD').click(function () {
          $('#chart1').highcharts({
            credits:{
                enabled:false
            },
            title: {
                text: '海淀二手交易'
            },
            yAxis: {
                title: {
                    text:  'TOP 5'
                }
            },
            series: {{ chart_HD|safe }}
        });
    });
    </script>

    <script>
        $('#TZ').click(function () {
          $('#chart1').highcharts({
            credits:{
                enabled:false
            },
            title: {
                text: '通州二手交易'
            },
            yAxis: {
                title: {
                    text:  'TOP 5'
                }
            },
            series: {{ chart_TZ|safe }}
        });
    });
    </script>

    <script>
    $(function () {
        $('#pie1').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie'
            },
            title: {
                text: '一天内交易物品种类分布'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    },
                    showInLegend: true
                }
            },
            series: [{
                name: 'Percent',
                colorByPoint: true,
                data: {{ pie1_data|safe }}
            }]
        });
    });
    </script>

    <script>
    $(function () {
        $('#pie2').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie'
            },
            title: {
                text: '一天内交易物品地区分布'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    },
                    showInLegend: true
                }
            },
            series: [{
                name: 'Percent',
                colorByPoint: true,
                data: {{ pie2_data|safe }}
            }]
        });
    });
    </script>

{% endblock %}

实现效果:

1.png
2.png
3.png
blog.png

总结:

  • 进一步学习了Django的框架,后续准备把document好好看看,买了本Python web开发测试驱动方法,准备系统学学。
  • 学习了用senmatic模版做网页,下一步需要学习html+css+javascript
  • 学习了如何搭建简单的网页。
  • 初步掌握了利用爬虫抓取数据,并进行网页的数据可视化。

课程结束了,学习才刚开始,继续学习,做些实际的项目,以后继续在博客中记录下来,谢谢老师。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容