Python實戰-20年內編程語言排名動態圖(超詳細,附源碼)


編程語言排名滾動效果實現。

 

工具

Python3
pyecharts
requests
正則表達式

 

數據獲取

數據來源:
https://www.tiobe.com/tiobe-index/

這家網站上記錄了每年每種語言的排名情況

 

 

 

獲取數據的代碼如下圖:

res = requests.get(self.url).text

通過requests模塊訪問網站URL實時獲取數據。

 

數據解析

當加載完網站數據時即可發現我們所需數據部分

 

 

 

這里直接附上操作解析數據代碼,主要通過正則表達式處理

def deal_data_html(self):
    date_list = list()
    return_data = []
    names = []
    res = requests.get(self.url).text
    content = ''.join(re.findall(r'series: (.*?)\}\);', res, re.DOTALL))
    content = re.findall(r'({.*?})', content, re.DOTALL)
    for line in content:
        name = ''.join(re.findall(r"{name : '(.*?)'", line))
        names.append(name)
        data = re.findall(r"\[Date.UTC(.*?)\]", line)
        for i_data in data:
            i_data = re.sub(r'[()]', "", i_data)
            value = i_data.split(',')[-1].strip()
            date = '-'.join(map(lambda x: x.strip(), i_data.split(',')[:-1]))
            if name == "Java":
                date_list.append(date)
            return_data.append({date: {name: value}})
    # print(return_data)
    # 賦值初始化
    msg_dict = {i: {j: 0 for j in names}for i in date_list}
    # print(msg_dict)
    # 給字典賦實際值
    for k, v in msg_dict.items():
        for i in return_data:
            for i_k, i_v in i.items():
                if k == i_k:
                    v.update(i_v)
    # print(msg_dict)

 

效果圖

接下來就是關鍵效果圖輸出了,這里采用了Pyecharts模塊包。具體如下圖所示。

 

實現代碼:

@staticmethod
def line_bar(msg_dict):
    t1 = Timeline().add_schema(play_interval=200)
    for date, msg in msg_dict.items():
        bar = (
            Bar()
            .add_xaxis(['C', 'Java', 'Python', 'C++', 'C#', 'Visual Basic', 'JavaScript', 'PHP', 'SQL', 'Assembly language'])
            .add_yaxis("語言熱門度", [msg[key] for key in msg.keys()], label_opts=opts.LabelOpts(position="right"))
            .reversal_axis()
            .set_global_opts(title_opts=opts.TitleOpts("20年編程語言排名 {}".format(date)),
                             legend_opts=opts.LegendOpts(is_show=False)
            )
        )
        t1.add(bar, date)
    t1.render("20年編程語言排名.html")

 

源碼請前往原文獲取:

https://mp.weixin.qq.com/s?__biz=Mzg3OTExODI3OA==&mid=2247483927&idx=1&sn=87547f528b852527378cc9ed8116f48a&chksm=cf0810bef87f99a8133096d962807b91bdb9f99e9f1227c22ccbf8449b6053a740490686811b&token=1106491029&lang=zh_CN#rd

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM