100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python模拟登录饿了么并爬取指定位置附近的商家信息

python模拟登录饿了么并爬取指定位置附近的商家信息

时间:2018-07-12 16:06:34

相关推荐

python模拟登录饿了么并爬取指定位置附近的商家信息

此篇内容使用到了以下模块:

1.使用openpyxl实现excel的读写

2.使用pypinyin实现汉子转拼音

3.使用geohash获取经纬度编码

4.使用requests中的utils方法实现cookies转字典和字典转cookies

5.使用json对列表和字典以及字符串之间进行转换

通过此次的学习可以爬取到全国2000多个城市指定位置附近的商家信息,如果再进一步挖掘的就具备了很大的商业价值

import requestsfrom pypinyin import lazy_pinyinimport geohashimport jsonimport openpyxlsession = requests.session()# 将处理后的信息写入excel文件中def write_info():rest_list = get_nearby_restaurants_by_menu()if rest_list != False:# 新建excelwb = openpyxl.Workbook()# 获取工作表sheet = wb.active# 工作表名称sheet.title = "饿了么商家信息"# 设置单元格的列宽sheet.column_dimensions[A].width = 40sheet.column_dimensions[B].width = 20sheet.column_dimensions[C].width = 60sheet.column_dimensions[D].width = 20sheet.column_dimensions[E].width = 30# 表头信息headers =[商家名称,月销量,商家活动,配送费,营业时间]sheet.append(headers)# 写入数据for rest in rest_list:sheet.append([rest[ ame],rest[order_num],rest[activities],rest[ship_fee],rest[open_hours]])# 保存数据wb.save("饿了么商家信息.xlsx")# 信息处理,获取所需要的信息def get_result(res):restaurants_infos = []for info in res:infos = {}name = info[ ame] # 商家名称recent_order_num_display = info[usiness_info]order_num = json.loads(recent_order_num_display)[ ecent_order_num_display] # 月销量open_hours = info[opening_hours][0] # 营业时间latitude = info[latitude] # 商家地理位置经纬度longitude = info[longitude]piecewise_agent_fee = info[piecewise_agent_fee][ ips] # 配送费activities = info[activities][0][ ips] # 活动infos[ ame] = nameinfos[order_num] = order_numinfos[open_hours] = open_hoursinfos[latitude] = latitudeinfos[longitude] = longitudeinfos[ship_fee] = piecewise_agent_feeinfos[activities] = activitiesrestaurants_infos.append(infos)# print(name + " " + str(order_num) + " " + str(open_hours) + " " + str(latitude) + " " + str(#longitude) + " " + str

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。