import requests
import json
import csv
def crawler_ele(page=0):
def get_page(page):
url = 'https://h5.ele.me/restapi/shopping/v3/restaurants?latitude=xxxx&longitude=xxxx&offset={page}&limit=8&terminal=h5'.format(page=page*8)
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36",
'cookie': r'xxxx'
}
re = json.loads(requests.get(url,headers=headers).text)
return re
re = get_page(page)
if re.get('items'):
with open('data.csv','a',newline='') as f:
writer = csv.DictWriter(f,fieldnames=['名称', '月销售量','配送费', '起送价', '风味','评分', '配送时长', '评分统计', '距离', '地址'])
writer.writeheader()
for item in re.get('items'):
info = dict()
restaurant = item.get('restaurant')
info['地址'] = restaurant.get('address')
info['配送费'] = restaurant.get('float_delivery_fee')
info['名称'] = restaurant.get('name')
info['配送时长'] = restaurant.get('order_lead_time')
info['距离'] = restaurant.get('distance')
info['起送价'] = restaurant.get('float_minimum_order_amount')
info['评分'] = restaurant.get('rating')
info['月销售量'] = restaurant.get('recent_order_num')
info['评分统计'] = restaurant.get('rating_count')
info['风味'] = restaurant.get('flavors')[0].get('name')
writer.writerow(info)
# print(info)
if re.get('has_next') == True:
crawler_page(page+1)
crawler_ele(0)
|