我用Python爬取美食網(wǎng)站3032個(gè)菜譜并分析,真香!
回復(fù)“書籍”即可獲贈(zèng)Python從入門到進(jìn)階共10本電子書


數(shù)據(jù)獲取
豆果美食網(wǎng)的數(shù)據(jù)爬取比較簡(jiǎn)單,如果您對(duì)爬蟲感興趣,可查看J哥往期原創(chuàng)文章「實(shí)戰(zhàn)|手把手教你用Python爬蟲(附詳細(xì)源碼)」,思路一致。
豆果美食網(wǎng)
本次爬取的數(shù)據(jù)范圍為川菜、粵菜、湘菜等八個(gè)中國(guó)菜系,包含菜譜名、鏈接、用料、評(píng)分、圖片等字段。限于篇幅,僅給出核心代碼。
1#?主函數(shù)
2def?main(x):
3????url?=?'https://www.douguo.com/caipu/{}/0/{}'.format(caipu,x*20)
4????print(url)
5????html?=?get_page(url)
6????parse_page(html,caipu)
7
8if?__name__?==?'__main__':
9????caipu_list?=?['川菜',?'湘菜','粵菜','東北菜','魯菜','浙菜','湖北菜','清真菜']?#中國(guó)菜系
10????start?=?time.time()??#?計(jì)時(shí)
11????for?caipu?in?caipu_list:
12????????for?i?in?range(22):
13????????????#?爬取多頁(yè)
14????????????main(x=i)
15????????????time.sleep(random.uniform(1,?2))
16????????????print(caipu,"第"?+?str(i+1)?+?"頁(yè)提取完成")
17????end?=?time.time()
18????print('共用時(shí)',round((end?-?start)?/?60,?2),?'分鐘')爬蟲核心代碼

數(shù)據(jù)清洗
導(dǎo)入數(shù)據(jù)
用pd.read方法導(dǎo)入爬取到的菜譜數(shù)據(jù),并添加列名。預(yù)覽數(shù)據(jù)如下:

刪除重復(fù)項(xiàng)
爬蟲過程中少量菜譜數(shù)據(jù)被重復(fù)抓取,需要用drop_duplicates方法刪除。

缺失值處理
通過info方法發(fā)現(xiàn)少量記錄含有缺失值,用dropna方法刪除。

評(píng)分字段清洗

添加用料數(shù)字段


數(shù)據(jù)可視化
本文數(shù)據(jù)可視化主要用到pyecharts庫(kù),它能輕松實(shí)現(xiàn)酷炫的圖表效果。如果您對(duì)可視化感興趣,可查看J哥往期原創(chuàng)文章「數(shù)據(jù)可視化分析系列」,涉及地產(chǎn)、電商、招聘等各領(lǐng)域。
菜譜評(píng)分分布
1from?pyecharts?import?options?as?opts
2from?pyecharts.charts?import?Page,?Pie
3cut?=?lambda?x?:?'4分以下'?if?x?4?else?('4.1-4.5分'?if?x?<=?4.5?else('4.6-4.9分'?if?x?<=?4.9?else?'5分'))
4df['評(píng)分分布']?=?df['評(píng)分'].map(cut)
5df2?=?df.groupby('評(píng)分分布')['評(píng)分'].count()
6df2?=?df2.sort_values(ascending=False)
7df2?=?df2.round(2)
8print(df2)
9c?=?(
10????????Pie()
11????????.add(
12????????????"",
13????????????[list(z)?for?z?in?zip(df2.index.to_list(),df2.to_list())],
14????????????radius=["20%",?"80%"],#?圓環(huán)的粗細(xì)和大小
15????????????rosetype='area'?#玫瑰圖
16????????)
17????????.set_global_opts(
18????????????title_opts=opts.TitleOpts(title="菜譜評(píng)分分布"
19?????????????????????????????????????),
20????????????legend_opts=opts.LegendOpts(
21????????????????orient="vertical",?pos_top="5%",?pos_left="2%"?,textstyle_opts=opts.TextStyleOpts(font_size=14)#?左面比例尺
22????????????),
23
24
25????????)
26????????.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:go7utgvlrp%",font_size=18),
27??????????????????????)
28????)
29c.render_notebook()
玫瑰圖代碼

菜譜評(píng)分分布玫瑰圖
各菜系菜譜數(shù)量對(duì)比
1from?pyecharts?import?options?as?opts
2from?pyecharts.charts?import?Page,?Pie?
3df2?=?df.groupby('菜系')['評(píng)分'].count()?#按菜系分組,對(duì)評(píng)分計(jì)數(shù)
4df2?=?df2.sort_values(ascending=False)?#降序
5print(df2)
6c?=?(
7????????Pie()
8????????.add("",?[list(z)?for?z?in?zip(df2.index.to_list(),df2.to_list())])
9????????.set_global_opts(title_opts=opts.TitleOpts(title="各菜系菜譜數(shù)量占比",subtitle="數(shù)據(jù)來(lái)源:豆果美食"))
10????????.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:?{c}"))
11????)
12c.render_notebook()
餅圖代碼

各菜系菜譜數(shù)量占比餅圖
各菜系評(píng)分對(duì)比
1from?pyecharts?import?options?as?opts
2from?pyecharts.charts?import?Page,?Pie
3df2?=?df.groupby('菜系')['評(píng)分'].mean()
4df2?=?df2.sort_values(ascending=False)
5df2?=?df2.round(2)
6print(df2)
7c?=?(
8????????Pie()
9????????.add(
10????????????"",
11????????????[list(z)?for?z?in?zip(df2.index.to_list(),df2.to_list())],
12????????????radius=["40%",?"75%"],???#?圓環(huán)的粗細(xì)和大小
13????????)
14????????.set_global_opts(
15????????????title_opts=opts.TitleOpts(title="各菜系平均評(píng)分"),
16????????????legend_opts=opts.LegendOpts(
17????????????????orient="vertical",?pos_top="5%",?pos_left="2%"??#?左面比例尺
18????????????),
19????????)
20????????.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
21????)
22c.render_notebook()
環(huán)狀圖代碼

各菜系平均評(píng)分環(huán)狀圖
各菜系用料數(shù)量對(duì)比
1from?pyecharts.charts?import?Bar,Pie
2from?pyecharts?import?options?as?opts
3df1?=?df.groupby('菜系')['用料數(shù)'].mean()?#按菜系分組,對(duì)評(píng)分計(jì)數(shù)
4df1?=?df1.sort_values(ascending=False)?#降序
5df1?=?df1.round(0)
6print(df1)
7bar?=?Bar()
8bar.add_xaxis(df1.index.to_list())
9bar.add_yaxis("用料數(shù)量",df1.to_list())
10bar.set_global_opts(title_opts=opts.TitleOpts(title="各菜系用料數(shù)量",subtitle="數(shù)據(jù)來(lái)源:豆果美食"))
11bar.render_notebook()
柱狀圖代碼

各菜系用料數(shù)量柱狀圖
川菜用料分析
1#?繪制詞云圖
2text1?=?get_cut_words(content_series=df[df['菜系']=='川菜']['用料'])
3stylecloud.gen_stylecloud(text='?'.join(text1),?max_words=1000,
4??????????????????????????collocations=False,
5??????????????????????????font_path='字酷堂清楷體.ttf',
6??????????????????????????icon_name='fas?fa-thumbs-up',
7??????????????????????????size=653,
8??????????????????????????output_name='./川菜.png')
9Image(filename='./川菜.png')?
詞云圖代碼

川菜用料詞云圖

川味砂鍋之足不出戶的麻辣燙? ? 圖片來(lái)源:豆果美食
粵菜用料分析

粵菜用料詞云圖

廣式腸粉? ??圖片來(lái)源:豆果美食
湘菜用料詞云圖

湘菜用料詞云圖

麻辣鹵鴨三件? ??圖片來(lái)源:互聯(lián)網(wǎng)
東北菜用料詞云圖

東北菜用料詞云圖

翡翠白菜水餃? ??圖片來(lái)源:豆果美食湖北菜用料詞云圖

湖北菜用料詞云圖

家常美味——香菇雞肉面? ??圖片來(lái)源:豆果美食
浙菜用料詞云圖

浙菜用料詞云圖

經(jīng)典糖醋排骨? ??圖片來(lái)源:豆果美食魯菜用料詞云圖

魯菜用料詞云圖

大白菜燉牛肉? ??圖片來(lái)源:豆果美食
清真菜用料詞云圖

清真菜用料詞云圖

糖醋蛋白肉? ??圖片來(lái)源:豆果美食聲明
1.本數(shù)據(jù)分析只做學(xué)習(xí)研究之用途,提供的結(jié)論僅供參考,美食的烹飪涉及的影響因素還有很多,請(qǐng)獨(dú)立思考;
2.作者與豆果美食無(wú)任何瓜葛,只是他家數(shù)據(jù)比較全面且干凈,便于數(shù)據(jù)分析,大家也可以去其他美食平臺(tái)看看;
3.作者對(duì)傳統(tǒng)美食文化了解甚微,相關(guān)描述可能存在不盡完善之處,請(qǐng)勿對(duì)號(hào)入座。
-------------------?End?-------------------
往期精彩文章推薦:
手把手教你使用Flask搭建ES搜索引擎(實(shí)戰(zhàn)篇)
簡(jiǎn)述Python、Anaconda、virtualenv和Miniconda之間的區(qū)別
【進(jìn)階篇】Python+Go——帶大家一起另尋途徑提高計(jì)算性能

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Python學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
/今日留言主題/
隨便說(shuō)一兩句吧~~
