一、统计明星粉丝Top30统计
从我们获取到的5万明星信息,可以统计出来粉丝数量排序以及展示,由于显示有限,我们只展示Top30,通过折线图来展示
from pyecharts import Bar,Line
import MySQLdb
class MostFans:
def __init__(self):
self.title = "明星粉丝Top30统计"
self.vicetitle = "数据统计来源于微博"
def charts(self):
datasource = self.dataSource()
if datasource:
line = Line(self.title, self.vicetitle,width="100%")
line.add("粉丝数", datasource["name"], datasource["fans"], mark_point=["average"])
line.add("关注数", datasource["name"], datasource["follow"], is_smooth=True, mark_line=["max", "average"])
line.render()
else:
print("未获取到数据,无法统计!")
def dataSource(self):
db = MySQLdb.connect("数据库IP", "数据库账号", "数据库密码", "数据库", charset='utf8')
cursor = db.cursor()
sql &#61; "SELECT &#96;id&#96;,&#96;name&#96;,&#96;weibo_followers_count&#96;,&#96;weibo_follow_count&#96; FROM new_star WHERE weibo_id <> 1 GROUP BY weibo_id ORDER BY &#96;weibo_followers_count&#96; DESC LIMIT %s" % (30)
try:
cursor.execute(sql)
results &#61; cursor.fetchall()
lists &#61; {"name":[],"fans":[],"follow":[]}
for result in results:
lists["name"].append(result[1])
lists["fans"].append(result[2])
lists["follow"].append(result[3])
return lists
except:
return []
if __name__ &#61;&#61; &#39;__main__&#39;:
modeclass &#61; MostFans()
modeclass.charts()
上面我们将爬虫获取到的数据通过dataSource获取&#xff0c;然后通过charts方法进行统计。从图表中我们可以轻松的看出&#xff0c;谢娜以1010500000粉丝占排行榜第一&#xff0c;同时何炅以929600000粉丝排行第二&#xff0c;Angelababy以856600000排行第三。