多协程抓取薄荷网食物热量 下篇

如题
from gevent import monkey
monkey.patch_all
import gevent,requests,openpyxl,time
from gevent.queue import Queue
from bs4 import BeautifulSoup
start=time.time()
headers={
‘User-Agent’:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36’
}
url_master=[‘http://www.boohee.com/food/view_menu’]
url_list=[]
url_dict={}
for i in range(1,11):
u_i=‘http://www.boohee.com/food/group/’+str(i)
url_master.append(u_i)

for i in url_master:
url_slave=[]
for j in range(1,11):
url_slave.append(i+’?page=’+str(j))
url_list.append(i+’?page=’+str(j))
url_dict[i]=url_slave

value_list=[]
for value in url_dict.values():
value_list.append(value)

work=Queue()
for url in url_list:
work.put_nowait(url)

content=[]
def spider():
global value_list,zero,one,two,three,four,five,six,seven,eigth,nine,ten
while not work.empty():
url=work.get_nowait()
if url in value_list[0]:
zero=res(url)
elif url in value_list[1]:
one=res(url)
elif url in value_list[2]:
two=res(url)
elif url in value_list[3]:
three=res(url)
elif url in value_list[4]:
four=res(url)
elif url in value_list[5]:
five=res(url)
elif url in value_list[6]:
six=res(url)
elif url in value_list[7]:
seven=res(url)
elif url in value_list[8]:
eigth=res(url)
elif url in value_list[9]:
nine=res(url)
elif url in value_list[10]:
ten=res(url)

def res(url):
res=requests.get(url,headers=headers)
#print(url,res.status_code)
soup=BeautifulSoup(res.text,‘html.parser’)
a=soup.find_all(‘div’,class_=‘text-box pull-left’)
for i in a:
title=i.find(‘a’)[‘title’]
href=‘http://www.boohee.com’+i.find(‘a’)[‘href’]
calorie=i.find(‘p’).text
content.append([title,href,calorie])
return content

def write(sheet,list):
for i in list:
sheet.append(i)

task_list=[]
for x in range(5):
task=gevent.spawn(spider)
task_list.append(task)
gevent.joinall(task_list)

wb=openpyxl.Workbook()
name_list=[‘菜肴’,‘谷薯芋、杂豆、主食’,‘蛋类、肉类及制品’,‘奶类及制品’,‘蔬果和菌藻’,‘坚果、大豆及制品’,‘饮料’,‘食用油、油脂及制品’,‘调味品’,‘零食、点心、冷饮’,‘其他’]
sheet=wb.active
sheet.title=name_list[0]
for i in range(1,11):
wb.create_sheet(name_list[i])
for i in name_list:
sheet=wb[i]
sheet[‘A1’]=‘菜名’
sheet[‘B1’]=‘链接’
sheet[‘C1’]=‘卡路里’

content_list=[zero,one,two,three,four,five,six,seven,eigth,nine,ten]
for i in range(11):
write(wb[name_list[i]],content_list[i])

wb.save(‘boohee.xlsx’)
wb.close()

end=time.time()
print(end-start)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>