我们使用pygal可以将数据转化成各种各样的图标,比数据库的表更有表现力
pygal文档: http://pygal.org/en/stable/
我们将赶集网爬取到的租房数据按照装修方式进行可视化
import pygal
from sqlalchemy import create_engine
engine = create_engine("sqlite:///ganji_zufang",echo=True)
from sqlalchemy import Table,Integer,String,ForeignKey,Column
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
DBSession = sessionmaker(bind=engine)
session = DBSession()
class ZuFang(Base):
__tablename__ = "zufang"
id = Column(Integer,primary_key=True)
title = Column(String(400))
price = Column(String(400))
size = Column(String(400))
boss = Column(String(400))
@classmethod
def save(cls,data):
session.add(data)
session.commit()
return data.id
Base.metadata.create_all(engine)
jzx = session.query(ZuFang).filter(ZuFang.size.contains("精装修")).count()
jdzx = session.query(ZuFang).filter(ZuFang.size.contains("简单装修")).count()
hhzx = session.query(ZuFang).filter(ZuFang.size.contains("豪华装修")).count()
#可视化部分
pie_chart = pygal.Pie()
pie_chart.title = '装修样式统计图'
pie_chart.add("精装修:{}".format(jzx), jzx)
pie_chart.add("简单装修:{}".format(jdzx), jdzx)
pie_chart.add("豪华装修:{}".format(hhzx), hhzx)
pie_chart.render_in_browser()