大學(xué)生都在吃什么——餓了么-松江大學(xué)城周邊外賣信息的數(shù)據(jù)分析

國慶回了一趟松江大學(xué)城,好吃的真是越來越多了,可惜只有一個(gè)胃?;貋砗笸蝗挥辛遂`感,不如分析看看大學(xué)城什么類型的美食最多吧!

為什么選擇餓了么這個(gè)平臺(tái)呢,因?yàn)楫?dāng)年外賣APP還沒有火的時(shí)候,我們就是在網(wǎng)頁版的餓了么上叫外賣的,也算是一種情懷吧~

數(shù)據(jù)來源平臺(tái):餓了么
地點(diǎn)選擇:松江大學(xué)城四期
抓取地址https://www.ele.me/place/wtw0tgvd7yr(翻頁鏈接:https://www.ele.me/restapi/shopping/restaurants?geohash=wtw0tgvd7yr&latitude=31.04641&limit=0&longitude=121.19791&offset=24&terminal=web
抓取數(shù)據(jù):只抓取了店名(name)和店的口味(flavors)

image.png

分析內(nèi)容:1、對(duì)店名進(jìn)行分詞然后詞頻統(tǒng)計(jì)繪制詞云圖,因?yàn)榻Y(jié)巴分詞對(duì)小吃的分詞不是很準(zhǔn)確,所以在分詞時(shí)加入了自己的字典fooddic;
2、對(duì)口味進(jìn)行統(tǒng)計(jì)繪制條形圖(偷懶沒有排序)

爬蟲和繪圖代碼:

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 11 09:05:59 2018

@author: Shirley
"""
import requests
import json
import re
import csv
from collections import defaultdict
import jieba
from wordcloud import WordCloud as wd#詞云
from PIL import Image#打開圖片,用于詞云背景層
import numpy as np#轉(zhuǎn)換圖片,用于詞云背景層
import matplotlib.pyplot as plt#繪圖
from matplotlib.font_manager import FontProperties#中文顯示
font = FontProperties(fname=r"D:\anaconda\shirleylearn\cipintongji\simsun.ttf", size=14)#設(shè)置中文字體

data = []
restaurants = []
foodtype = []
def Getdata(page):#爬蟲

    url = "https://www.ele.me/restapi/shopping/restaurants?geohash=wtw0tgvd7yr&latitude=31.04641&limit=24&longitude=121.19791&offset=%d&terminal=web"%page

    headers = {"accept":"application/json, text/plain, */*",
               "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36",
               "cookie":"登陸后的cookie"}
    html = requests.get(url,headers=headers)

    content = re.findall(r'"flavors":.*?,"next_business_time"',html.text)#用正則獲取包含數(shù)據(jù)的那部分

    for con in content:
        jsonstring = "{" + con.replace(',"next_business_time"',"}")#完善格式,使其成為準(zhǔn)確的json格式
        jsonobj = json.loads(jsonstring)
        restaurant_id = jsonobj["id"]
        restaurant_name = jsonobj["name"].encode("gbk","ignore").decode("gbk")
        flavors = jsonobj["flavors"]
        restaurant_type = []
        for f in flavors:#有些flavors中只有一個(gè)值,有些有2個(gè),所以要for循環(huán)
            restaurant_type.append(f["name"])
        restaurants.append(restaurant_name)#用于后面詞云圖
        foodtype.append(restaurant_type)#用于后面條形圖
        data.append([restaurant_id,restaurant_name,restaurant_type])
    
    with open("elemedata.csv","w",newline="") as f:#保存數(shù)據(jù)到本地
        writer = csv.writer(f)
        writer.writerow(["restaurant_id","restaurant_name","restaurant_type"])
        for d in data:
            writer.writerow(d)
    
    return restaurants,foodtype#返回值應(yīng)用到下面2個(gè)函數(shù)


def Eleme_wordcloud(restaurants):#詞云圖
    jieba.load_userdict("D:/anaconda/shirleylearn/eleme/fooddic.txt")
    text = ""
    for i in restaurants:
        name = re.sub(r'(.*',"",i)
        name = re.sub(r'\(.*',"",name)
    
        text = text + " " + name

    fenci = jieba.lcut(text)

    wordfrequency = defaultdict(int)
    for word in fenci:
        if word != " ":
            wordfrequency[word] += 1#詞頻統(tǒng)計(jì)
    
    img = Image.open("D:/anaconda/shirleylearn/eleme/bowl.jpg")#打開圖片
    myimg = np.array(img)#轉(zhuǎn)換圖片

    path = "D:/anaconda/shirleylearn/eleme/simsun.ttf"
    wordcloud = wd(width=1000,height=860,margin=2,font_path=path,background_color="white",max_font_size=100,mask = myimg).fit_words(wordfrequency)#根據(jù)詞頻字典生成詞云
    plt.imshow(wordcloud)
    plt.axis('off')#不顯示坐標(biāo)軸 
    plt.savefig('eleme_wordcloud.png', dpi=300)
    plt.clf()# 清除當(dāng)前 figure 的所有axes,但是不關(guān)閉這個(gè) window,所以能繼續(xù)復(fù)用于其他的 plot。否則會(huì)影響下面的繪圖
    
def Eleme_bar(foodtype):#條形圖
    #foodtype的格式:[['蓋澆飯', '簡(jiǎn)餐'],['川湘菜', '簡(jiǎn)餐'],['日韓料理']]
    wordfrequency2 = defaultdict(int)
    foodtypes = []#放總的類型,有重復(fù)項(xiàng)
    types = []#放詞頻統(tǒng)計(jì)后的類型,無重復(fù)項(xiàng)
    numbers = []#放詞頻統(tǒng)計(jì)后的詞頻
    for f in foodtype:
        for t in f:
            foodtypes.append(t)#把每個(gè)詞匯總到列表中
    for type in foodtypes:
        wordfrequency2[type] += 1#用字典進(jìn)行詞頻統(tǒng)計(jì)
    for key in wordfrequency2:
        types.append(key)
        numbers.append(wordfrequency2[key])
    
    plt.bar(range(len(types)),numbers)
    plt.xticks(range(len(types)),types,fontproperties = font,fontsize=5,rotation=90)
    plt.savefig('eleme_bar.png', dpi=300)
    plt.show()   
    plt.clf()
    
if __name__ == '__main__':
    for p in range(0,24):
        page = p*24
        restaurants,foodtype = Getdata(page)
    Eleme_wordcloud(restaurants)
    Eleme_bar(foodtype)

運(yùn)行中遇到的問題:運(yùn)行界面只顯示了條形圖,沒有顯示詞云圖,不過保存下來的圖是正確的。

詞云圖

eleme_wordcloud.png

看起來在大學(xué)城米飯類要比面食類更受歡迎,粥、香鍋、麻辣燙也是我讀大學(xué)的時(shí)候經(jīng)常吃的。
對(duì)于鮮花這個(gè)詞,我查看了原始數(shù)據(jù),確實(shí)外送的花店比較多,但是距離都較遠(yuǎn),實(shí)際上大多可以排除在大學(xué)城外。

條形圖

eleme_bar.png

除簡(jiǎn)餐外,蓋澆飯、地方小吃、米粉面館是三巨頭,甜品、奶茶和炸雞也是大家的心頭好。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容