#目标:用多协程爬到薄荷网的食物热量数据。
from gevent import monkey
monkey.patch_all
from bs4 import BeautifulSoup
from gevent.queue import Queue
import gevent,time,csv,requests
#时间戳
sta=time.time()
#c创建文档
file=open(r’food_calorie1.csv’,‘w’,newline=’’,encoding=‘utf-8’)
writer=csv.writer(file)
#获得网址,创建队列,添加网址
work=Queue()
i=‘view_menu’
for j in range(1,4):
url=‘http://www.boohee.com/food/’+str(i)+’?page=’+str(j)
work.put_nowait(url)
for i in range(1,4):
for j in range(1,4):
work.put_nowait(url)
print(len(work))
#定义函数,获得数据,解析数据
def crawler():
while not work.empty():
url=work.get_nowait()
raw_headers=’’‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9