大家好,我是天空之城,今天給大家?guī)砣绾胃鶕?jù)爬蟲獲得的食物卡路里、自身基礎(chǔ)熱量和運(yùn)動(dòng)消耗量計(jì)算總的熱量差,這里涉及到了爬蟲知識(shí),數(shù)據(jù)的清洗和處理,以及簡單的邏輯判斷和計(jì)算。總的思路就是用獲取的食物熱量減去自身的基礎(chǔ)熱量和運(yùn)動(dòng)消耗的熱量得到人體剩余的熱量差。
先看下我們最后得到的效果圖
輸入食物名稱,后臺(tái)就已經(jīng)獲得了食物熱量,
再輸入性別等參數(shù),就會(huì)計(jì)算出人體基礎(chǔ)熱量,
再輸入運(yùn)動(dòng)量,
經(jīng)過計(jì)算就會(huì)得到總共消耗的卡路里,
以及體內(nèi)還剩余的卡路里
第一步,首先是通過爬蟲獲得所有食物的卡路里數(shù)據(jù)
import gevent,time,requests from bs4 import BeautifulSoup from gevent.queue import Queue from gevent import monkey monkey.patch_all() import openpyxl start=time.time() wb=openpyxl.Workbook() sheet=wb.active sheet.title='食物熱量表' sheet['A1']='食物名稱' sheet['B1']='網(wǎng)址' sheet['C1']='食物熱量' url_list=[] for k in range(1,11): for j in range(1, 11): url_list.append('http://www.boohee.com/food/group/{}?page={}'.format(str(k),str(j))) for h in range(1,11): url_list.append('http://www.boohee.com/food/view_menu?page={}'.format(str(h))) #print(url_list) headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0', 'Referer':'http://www.boohee.com/food/group/1', 'Cookie':'Hm_lvt_7263598dfd4db0dc29539a51f116b23a=1588486427; Hm_lpvt_7263598dfd4db0dc29539a51f116b23a=1588487435' } work=Queue() for url in url_list: work.put_nowait(url) def getdata(): while not work.empty(): url=work.get_nowait() res=requests.get(url,headers=headers) #print(res.status_code) soup = BeautifulSoup(res.text, 'html.parser') items = soup.find('ul', class_='food-list') list = items.find_all('li') for film in list: name = film.find('h4').text link = film.find('a')['href'] wzlink='http://www.boohee.com'+link reliang = film.find('p').text print(name, wzlink, reliang) row=[name,wzlink,reliang] sheet.append(row) wb.save('食物熱量對照表.xlsx') task_list=[] for x in range(5): task=gevent.spawn(getdata) task_list.append(task) gevent.joinall(task_list) end=time.time() print(end-start)
獲得excel數(shù)據(jù)截圖如下
第二步,數(shù)據(jù)清洗和處理
對食物名稱和卡路里經(jīng)過簡單數(shù)據(jù)處理,獲得如下表格
食物從A列處理成B列(對食物名稱進(jìn)行精簡,方便后面進(jìn)行判斷),食物熱量從D列處理成E列(變成數(shù)值,方面后面計(jì)算)
第三步,程序?qū)崿F(xiàn)過程,對數(shù)據(jù)進(jìn)行讀取和判斷過程。
想要的效果就是,先輸入你吃的食物名稱,獲取到相應(yīng)的食物熱量
import pandas as pd # df=pd.DataFrame(pd.read_csv('name.csv',header=1)) df=pd.DataFrame(pd.read_excel('食物熱量對照表1.xlsx')) # print(df['食物名稱'][0,10]) # print(df['食物熱量1'][0,10]) # print(df.iloc[0,0]) # print(df.iloc[1,0]) dict={} list=[] for i in range(1100): # print(df.iloc[i, 1]) # print(df.iloc[i, 4]) a=df.iloc[i, 1] b=df.iloc[i, 4] dict[a]=b # list.append(dict) print(dict) while True: food=input('請輸入你的食物名稱:') if food in dict: print(dict[food]) else: print('你輸入的食物不存在') break
第四步,熱量計(jì)算過程
在第三步中,我們獲得了食物的熱量,第四步就是輸入人的性別,身高,體重和年齡計(jì)算基礎(chǔ)熱量,輸入走的步數(shù)得到消耗熱量,再用食物熱量減去上述兩個(gè)熱量最后得到熱量差,大功告成。
y_or_n = input('是否退出程序y/n?') while y_or_n != 'y': # '男 80 180 16' str_imfor = input('請您輸入性別,體重(kg),身高(cm),年齡用空格隔開:') list_imfor = str_imfor.split(' ') print(list_imfor) gender = list_imfor[0] print(gender) weight = float(list_imfor[1]) print(weight) height = float(list_imfor[2]) print(height) age = int(list_imfor[3]) print(age) if gender =='男': bmr = (13.7*weight)+(5*height)-(6.8*age)+66 elif gender =='女': bmr = (9.6 * weight) + (1.8 * height) - (4.7 * age) + 655 else: bmr = -1 if bmr != -1: print("基礎(chǔ)熱量={",bmr,'}') else: print('暫時(shí)不支持該性別') walk = int(input('輸入你走的步數(shù):')) calories = walk * 28 print('今天共消耗卡路里:',calories+bmr) y_or_n = input('是否退出程序y/n?')
完整的程序代碼
import pandas as pd # df=pd.DataFrame(pd.read_csv('name.csv',header=1)) def foodca(): df=pd.DataFrame(pd.read_excel('食物熱量對照表1.xlsx')) # print(df['食物名稱'][0,10]) # print(df['食物熱量1'][0,10]) # print(df.iloc[0,0]) # print(df.iloc[1,0]) dict={} list=[] for i in range(1100): # print(df.iloc[i, 1]) # print(df.iloc[i, 4]) a=df.iloc[i, 1] b=df.iloc[i, 4] dict[a]=b # list.append(dict) # print(dict) food=input('請輸入你的食物名稱:') if food in dict: calorie_food=dict[food] # print(calorie_food) return calorie_food else: print('你輸入的食物不存在') def main(calorie_food): y_or_n = 'n' while y_or_n != 'y': # '男 80 180 16' str_imfor = input('請您輸入性別,體重(kg),身高(cm),年齡用空格隔開:') list_imfor = str_imfor.split(' ') # print(list_imfor) gender = list_imfor[0] # print(gender) weight = float(list_imfor[1]) # print(weight) height = float(list_imfor[2]) # print(height) age = int(list_imfor[3]) # print(age) if gender =='男': bmr = (13.7*weight)+(5*height)-(6.8*age)+66 elif gender =='女': bmr = (9.6 * weight) + (1.8 * height) - (4.7 * age) + 655 else: bmr = -1 if bmr != -1: print("基礎(chǔ)大卡={",bmr,'}') else: print('暫時(shí)不支持該性別') walk = int(input('請輸入你走的步數(shù):')) calories = walk * 28 print('今天共消耗卡路里是:',(calories+bmr)*0.001,'大卡') print('今天還??防锸?,calorie_food-(calories+bmr)*0.001,'大卡') y_or_n = input('是否退出程序y/n?') if __name__ == '__main__': y_or_n = input('是否退出程序y/n?') if y_or_n == 'n': mess=foodca() main(mess) else: print('歡迎下次使用') pass
最后的效果圖