一、实训目标
认识数据库的构建、ketle数据洗濯,把握Python编程技术实现网络爬虫收罗数据、统计分析与可视化。
二、实训内容
(一)模仿点评网站的摆设
(二)网络数据爬取和数据的洗濯
(三)数据统计分析和可视化
三、实训设备要求
一个win10体系,一个ubuntu体系
四、实训操纵步骤
(一)模仿点评网站摆设
1.安装python3.6
- sudo apt-get install python3.6
复制代码
2.安装apach
- sudo apt-get install apache2
复制代码
3.安装Django1.9
4.设置apache2
vim /etc/apache2/sites-available/000-default.conf
5.安装和设置mysql
sudo cat /etc/mysql/debian.cnf
vim /home/joker/spider/spider/settings.py
6.安装restframework
pip3 install djangorestramework==3.4.4
7.安装pymysql
sudo apt install python-pip
8.启动apache2
/spider$ python3 manage.py runserver 192.168.182.141:8001
(二)网络数据爬取和数据的洗濯
1.新建数据库
- import pymysql
- # 此类操作数据库
- class DBConnect():
- #这个类用于连接数据库、插入数据、查询数据
- def __init__(self):
- self.host = '192.168.182.141'
- self.port = 3306
- self.user = 'root'
- self.passwd = '123456'
- self.db = 'testdazhong'
- # 连接到具体的数据库
- def connectDatabase(self):
- conn = pymysql.connect(host=self.host,
- port=self.port,
- user=self.user,
- passwd=self.passwd,
- db=self.db,
- charset='utf8')
- return conn
- # 插入数据
- def insert(self, sql, *params):
- conn = self.connectDatabase()
- cur = conn.cursor()
- cur.execute(sql, params)
- conn.commit()
- cur.close()
- conn.close()
- # 查询数据
- def select(self, sql):
- conn = self.connectDatabase()
- cur = conn.cursor()
- try:
- # 执行SQL语句
- cur.execute(sql)
- conn.commit()
- # 获取所有记录列表
- results = cur.fetchall()
- return results
- except:
- print("Error: unable to fetch data")
- cur.close()
- conn.close()
复制代码  3. kettle数据洗濯

(三)统计分析与可视化
- from pyecharts import Line, Bar, Page, WordCloud
- from spider import dbconnect
- #查询商铺名称、计算综合评价分数=(口味+环境+服务)/3的函数
- def evaluateshop(city):
- #连接数据库
- dbcon = dbconnect.DBConnect()
- dbcon.connectDatabase();
- #定义查询语句,查询商铺名称、计算综合评价分数
- nsql = '''SELECT shopName,(tasteScore+environmentScore+serviceScore)/3.0 as avg from dazhongoutput WHERE city="%s" ORDER BY (tasteScore+environmentScore+serviceScore)/3.0 DESC;'''%(city)
- LData =dbcon.select(nsql)
- LShopName = []
- LAvg = []
- #返回商铺名称、综合评价分数
- for idata in LData:
- LShopName.append(idata[0])
- LAvg.append(round(idata[1], 2))
- return [LShopName,LAvg]
- #TOP20商铺综合分数画图函数
- def plottop20(score_y1, score_y2, score_y3):
- #定义页面
- page = Page()
- #折线图定义、添加数据和配置项
- line1 = Line("济南饭店评分统计", "数据来源于点评网TOP100", width=1200)
- line1.add("综合", score_y1[0][:20], score_y1[1][:20], is_label_show=True, is_more_utils=True)
- line2 = Line("青岛饭店评分统计", "数据来源于点评网TOP100", width=1200)
- line2.add("综合", score_y2[0][:20], score_y2[1][:20], is_label_show=True, is_more_utils=True)
- line3 = Line("威海饭店评分统计", "数据来源于点评网TOP100", width=1200)
- line3.add("综合", score_y3[0][:20], score_y3[1][:20], is_label_show=True, is_more_utils=True)
- #页面添加图形
- page.add(line1)
- page.add(line2)
- page.add(line3)
- #生成HTML,数据可视化展示
- page.render("山东3大城市TOP20商铺综合服务评分.html")
- if __name__ == '__main__':
- #评估3大城市商铺综合分数
- shop1 = evaluateshop("济南")
- shop2 = evaluateshop("青岛")
- shop3 = evaluateshop("威海")
- #建立数据列表画图,生成HTML
- score_x = []
- score_y1, score_y2, score_y3 = [], [], []
- for data, data2, data3 in zip(shop1, shop2, shop3):
- score_y1.append(data)
- score_y2.append(data2)
- score_y3.append(data3)
- plottop20(score_y1, score_y2, score_y3)
复制代码

- from pyecharts import Pie, Page,Bar
- from spider import dbconnect
- #查询统计某城市菜品分类名
- def foodClass(city):
- #连接数据库
- dbcon = dbconnect.DBConnect()
- dbcon.connectDatabase();
- fsql = '''SELECT COUNT(mainCategoryName),mainCategoryName from dazhongoutput where city="%s" GROUP BY mainCategoryName;'''%(city)
- #查询获得菜品分类商店梳理、菜品分类名称数据返回
- LData = dbcon.select(fsql)
- return LData
- #菜品分类画图函数
- def plotfoodclass():
- #查询统计济南的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("济南")]
- [Laddress.append(i[1]) for i in foodClass("济南")]
- print(Laddress)
- print(Lsum)
- #定义页面
- page = Page()
- #添加饼图的数据和配置项
- fpie1 = Pie("济南菜品分类", title_pos='center')
- fpie1.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- #添加柱状图的数据和配置项
- fbar1 = Bar("济南菜品分类", "数据来源于点评网TOP100",title_pos ='center')
- fbar1.add("济南", Laddress, Lsum, mark_point=["max", "min"],legend_pos="right")
- # 查询统计青岛的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("青岛")]
- [Laddress.append(i[1]) for i in foodClass("青岛")]
- print(Laddress)
- print(Lsum)
- # 添加饼图的数据和配置项
- fpie2 = Pie("青岛菜品分类", title_pos='center')
- fpie2.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- # 添加柱状图的数据和配置项
- fbar2 = Bar("青岛菜品分类", "数据来源于点评网TOP100", title_pos='center')
- fbar2.add("青岛", Laddress, Lsum, mark_point=["max", "min"], legend_pos="right")
- # 查询统计威海的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("威海")]
- [Laddress.append(i[1]) for i in foodClass("威海")]
- print(Laddress)
- print(Lsum)
- # 添加饼图的数据和配置项
- fpie3 = Pie("威海菜品分类", title_pos='center')
- fpie3.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- # 添加柱状图的数据和配置项
- fbar3 = Bar("威海菜品分类", "数据来源于点评网TOP100", title_pos='center')
- fbar3.add("威海", Laddress, Lsum, mark_point=["max", "min"], legend_pos="right")
- #页面上添加图形
- page.add(fpie1)
- page.add(fbar1)
- page.add(fpie2)
- page.add(fbar2)
- page.add(fpie3)
- page.add(fbar3)
- page.render("山东3大城市菜品分类.html")
- if __name__ == '__main__':
- plotfoodclass()
复制代码

- from pyecharts import Pie, Page,Bar
- from spider import dbconnect
- #查询统计某城市菜品分类名
- def foodClass(city):
- #连接数据库
- dbcon = dbconnect.DBConnect()
- dbcon.connectDatabase();
- fsql = '''SELECT COUNT(mainCategoryName),mainCategoryName from dazhongoutput where city="%s" GROUP BY mainCategoryName;'''%(city)
- #查询获得菜品分类商店梳理、菜品分类名称数据返回
- LData = dbcon.select(fsql)
- return LData
- #菜品分类画图函数
- def plotfoodclass():
- #查询统计济南的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("济南")]
- [Laddress.append(i[1]) for i in foodClass("济南")]
- print(Laddress)
- print(Lsum)
- #定义页面
- page = Page()
- #添加饼图的数据和配置项
- fpie1 = Pie("济南菜品分类", title_pos='center')
- fpie1.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- #添加柱状图的数据和配置项
- fbar1 = Bar("济南菜品分类", "数据来源于点评网TOP100",title_pos ='center')
- fbar1.add("济南", Laddress, Lsum, mark_point=["max", "min"],legend_pos="right")
- # 查询统计青岛的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("青岛")]
- [Laddress.append(i[1]) for i in foodClass("青岛")]
- print(Laddress)
- print(Lsum)
- # 添加饼图的数据和配置项
- fpie2 = Pie("青岛菜品分类", title_pos='center')
- fpie2.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- # 添加柱状图的数据和配置项
- fbar2 = Bar("青岛菜品分类", "数据来源于点评网TOP100", title_pos='center')
- fbar2.add("青岛", Laddress, Lsum, mark_point=["max", "min"], legend_pos="right")
- # 查询统计威海的菜品分类以及数量
- Laddress = []
- Lsum = []
- [Lsum.append(i[0]) for i in foodClass("威海")]
- [Laddress.append(i[1]) for i in foodClass("威海")]
- print(Laddress)
- print(Lsum)
- # 添加饼图的数据和配置项
- fpie3 = Pie("威海菜品分类", title_pos='center')
- fpie3.add("", Laddress, Lsum, radius=[30, 75], label_text_color=None,
- is_label_show=True, legend_orient='vertical',
- legend_pos='auto', is_legend_show=False)
- # 添加柱状图的数据和配置项
- fbar3 = Bar("威海菜品分类", "数据来源于点评网TOP100", title_pos='center')
- fbar3.add("威海", Laddress, Lsum, mark_point=["max", "min"], legend_pos="right")
- #页面上添加图形
- page.add(fpie1)
- page.add(fbar1)
- page.add(fpie2)
- page.add(fbar2)
- page.add(fpie3)
- page.add(fbar3)
- page.render("山东3大城市菜品分类.html")
- if __name__ == '__main__':
- plotfoodclass()
复制代码 五、总结及心得体会
在举行大数据爬取可视化分析的实训过程中,我深刻体会到了数据在当今社会的重要性和价值。通过对大数据的爬取、洗濯、分析和可视化,我们能够从海量的数据中发掘出有价值的信息,为决定提供支持,推动企业发展,甚至改变社会。首先,在实训过程中,我学会了怎样使用Python等编程语言来举行数据爬取。通过编写爬虫程序,我能够自动化地从网页上抓取数据,实现大规模数据的获取。这让我深刻体会到了数据爬取的便捷和高效,同时也认识到了数据爬取中大概遇到的反爬步伐和解决方法。其次,数据洗濯是数据分析的重要一环。在实训中,我学会了怎样对爬取得到的数据举行洗濯和预处置惩罚,包括去除重复值、处置惩罚缺失值、统一数据格式等操纵。只有经过洗濯的数据才能够被准确地分析和可视化,否则会影响终极的结果和结论。接着,数据分析是实训的焦点环节。通过对洗濯后的数据举行统计分析、机器学习等方法,我们可以发现数据之间的关联、规律和趋势,从而得出有价值的结论。数据分析需要联合范畴知识和数据发掘技术,通过创建模子和算法来解决实际标题,这也是我在实训中不停探索和学习的方向。末了,数据可视化是将数据分析结果以图形化的方式展示出来,让人们更直观地明白数据背后的含义。在实训中,我学会了怎样使用可视化工具如Matplotlib、Seaborn、Tableau等来出现数据,包括制作折线图、柱状图、热力图等,以及怎样选择符合的图表类型来展示差别类型的数据。总的来说,通过这次大数据爬取可视化分析的实训,我不但学会了数据处置惩罚和分析的根本技能,还深刻认识到了数据对于决媾和创新的重要性。在未来的学习和工作中,我将继续深耕数据范畴,不停提升本身的数据分析能力,为实现数据驱动的智能决定贡献本身的力量。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |