记录美团美食爬虫采集过程 美团
分类 地址 平均价格 平均评分 商家名 服务 电话 营业时间 城市 区/县 评价数 评价标签
运行环境
python3.6
requests
打开美团 F12进入浏览器开发者模式。向下滚动,由于是动态加载的所以我们要找到那个加载请求,可以肉眼识别,也可以点击左上角的请求搜索关键词。
分析请求后发现请求地址为: http://meishi.meituan.com/i/api/channel/deal/list
是一个post请求,请求的参数如下数据所示,多点击几家店铺就能看出参数的含义。
{
"uuid": "106aa05a-ff4e-43bf-85e7-8c0a093b1473", # 网站分发的id
"version": "8.2.0",
"platform": 3,
"app": "",
"partner": 126,
"riskLevel": 1,
"optimusCode": 10,
"originUrl": "http://meishi.meituan.com/i/?ci=83&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1",
"offset": 15, # 偏移量 每次偏移15
"limit": 30, # 每次获取数量
"cateId": 1, # 菜式类型
"lineId": 0,
"stationId": 0,
"areaId": 0, # 地区类型 ,默认0为附近商家
"sort": "default", # "default": "智能排序", "distance": "离我最近", "avgscore": "好评优先", "solds": "人气最高",
"deal_attr_23": "", # 107: "只看免预约"
"deal_attr_24": "", # 109: "节假日可用"
"deal_attr_25": "", # 115: "单人餐", 117: "双人餐", 111: "3-4人餐", 112: "5-10人餐", 110: "10人餐以上"
"poi_attr_20043": "", # 20122: "早餐" , 20123: "午餐", 20124:"下午茶", 20125: "晚餐", 20126: "夜宵"
"poi_attr_20033": "" # 20062: "买单", 20063: "在线点菜", 20064: "外卖送餐", 20065: "在线排队", 20135: "预定"
}
直接模拟浏览器发送post请求,修改offset来实现翻页,每页有15条数据,每翻一页 offset值加15。实测在当前美食页面下直接翻页,最多能翻67页,1005条数据,后面好像出验证码还是没数据返回了。所以我们要对店铺进行分类抓取。
调整cookie
进行城市修改
关联ID
{
"ci": "83", # 城市代码
"cityname": "%E5%8C%97%E4%BA%AC" # 城市名称
}
调整请求参数areaId
可对地区修改
"areaId": 0
地区ID
import requests
import json
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36',
'Referer': 'http://meishi.meituan.com/i/?ci=20&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1',
}
request_payload = {
"uuid": "106aa05a-ff4e-43bf-85e7-8c0a093b1473",
"version": "8.2.0",
"platform": 3,
"app": "",
"partner": 126,
"riskLevel": 1,
"optimusCode": 10,
"originUrl": "http://meishi.meituan.com/i/?ci=83&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1",
"offset": 15, # 偏移量 每次偏移15
"limit": 1, # 每次获取数量
"cateId": 1, # 菜式类型
"lineId": 0,
"stationId": 0,
"areaId": 0, # 地区类型 ,默认0为附近商家
"sort": "default", # "default": "智能排序", "distance": "离我最近", "avgscore": "好评优先", "solds": "人气最高",
"deal_attr_23": "", # 107: "只看免预约"
"deal_attr_24": "", # 109: "节假日可用"
"deal_attr_25": "", # 115: "单人餐", 117: "双人餐", 111: "3-4人餐", 112: "5-10人餐", 110: "10人餐以上"
"poi_attr_20043": "", # 20122: "早餐" , 20123: "午餐", 20124:"下午茶", 20125: "晚餐", 20126: "夜宵"
"poi_attr_20033": "" # 20062: "买单", 20063: "在线点菜", 20064: "外卖送餐", 20065: "在线排队", 20135: "预定"
}
url = 'https://meishi.meituan.com/i/api/channel/deal/list'
cookies = {
"ci": "83", "cityname": "%E5%8C%97%E4%BA%AC"}
response = requests.post(url, data=json.dumps(request_payload), headers=headers, cookies=cookies)
list_data = json.loads(response.text)
列表页返回数据
{
'status': 0,
'data': {
'poiList': {
'totalCount': 1000,
'poiInfos': [{
'avgPrice': 63,
'avgScore': 5,
'cateName': '江西菜',
'channel': 'food',
'showType': '',
'frontImg': 'https://img.meituan.net/msmerchant/fe4008ec95a148cc73e0fb24e3e7be4d387750.jpg',
'lat': 28.6845,
'lng': 115.85443,
'name': '老房子(绿地店)',
'poiid': '105236836',
'areaName': '绿地中心',
'poiImgExtra': {
},
'extraServiceTags': [{
'icon': '',
'text': {
'content': '支持外卖',
'color': '#6cbab2',
'backgroundColor': ''
}
}],
'rotationTags': [],
'smartTags': [{
'icon': '',
'text': {
'content': '绿地中心美食人气榜第2名',
'color': '#be9e4d',
'backgroundColor': '#fbf4e4',
'borderColor': ''
}
}, {
'icon': '',
'text': {
'content': '回头客多',
'color': '#be9e4d',
'backgroundColor': '#fbf4e4',
'borderColor': ''
}
}],
'preferentialInfo': {
'hiddenNumber': 0,
'maidan': {
'defaultShowNum': 2,
'entries': [{
'discount': '',
'discountColor': '#38c2aa',
'content': '3-4人餐108元起,6-8人餐368元',
'icon': 'http://p1.meituan.net/codeman/93231059874052e97c0976c8a6e30dbe910.png',
'promotion': '',
'hasBorder': 0,
'borderColor': ''
}, {
'discount': '',
'discountColor': '#ff8822',
'content': '45代50元,90代100元',
'icon': 'http://p1.meituan.net/codeman/5b202af2ecf82c69a433a246296ca4791278.png',
'promotion': '',
'hasBorder': 0,
'borderColor': ''
}]
},
'combo': {
'defaultShowNum': 0,
'entries': []
}
},
'ctPoi': '069874187176495843190179878107216159651_a105236836_c0_e7062648939967934671',
'extraServiceIcons': [],
'distance': ''
}]
},
'notification': '',
'picassoViews': [],
'traceData': {
'_apimeishi_rerank_report_data': ''
}
}
}
我们需要的信息在店铺的详情页面,一般详情页面的url都是几个关键参数的拼凑,而这几个关键参数是可以在上面的列表页面抓取到的。我们点开一家店铺,观察url: https://meishi.meituan.com/i/poi/105236836?ct_poi=069874187176495843190179878107216159651_a105236836_c0_e1615922891919575994,发现主要是两个参数,一个是店铺的id:6268902,还有一个就是ct_poi参数,这两个参数都可以在上面的post请求返回数据中找到。
返回的是html里参杂json数据,按照各自的字段需求进行解析就可以了;
import requests
import json
cookies = {
"ci": "1", # 城市代码
"cityname": "%E5%8C%97%E4%BA%AC" # 城市名称
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36',
'Referer': 'http://meishi.meituan.com/i/?ci=20&stid_b=1&cevent=imt%2Fhomepage%2Fcategory1%2F1',
}
poiInfos = json.loads(response.text)['data']['poiList']['poiInfos']
for poiInfo in poiInfos:
ctPoi = poiInfo['ctPoi']
poiid = poiInfo['poiid']
cateName = poiInfo['cateName']
name = poiInfo['name']
city = cookies['cityname']
detail_url = f'https://meishi.meituan.com/i/poi/{poiid}?ct_poi={ctPoi}'
res = requests.get(
url=detail_url,
headers=headers,
cookies=cookies,
)
print(res.text)
水平有限,如有错误望指正
v:17610352720 爬虫 数据分析 可视化