获得美团的所有店铺信息

使用方式

改正cookie就可以使用

源代码


import pymysql import traceback import requests import json class splider: def __init__(self): # 这里的二级域名写死是 南宁(nn) self.searchUrl = "http://apimobile.meituan.com/group/v4/poi/pcsearch/99?" self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36", "Cookie": "uuid=8e5efbb68b0f42f89d2c.1521816215.1.0.0; ci=10; rvct=10; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_cuid=16253509d85c8-013d07a3bad9bd-3a614f0b-144000-16253509d8542; __mta=250385337.1521816217474.1521816217474.1521816217474.1; _lxsdk_s=16253509d87-fd8-4cc-e63%7C%7C2" } # 打开数据库连接(ip/数据库用户名/登录密码/数据库名),注意这里的编码格式,否则和python2.7一直乱码 self.db = pymysql.connect("localhost", "root", "123456", "meishi", charset='utf8') # 使用 cursor() 方法创建一个游标对象 cursor self.cursor = self.db.cursor() self.brand_ist=[] # 关闭数据库 def close_db(self): self.db.close() self.cursor.close() def getAllShopCount(self): with open("brand.txt", 'r', encoding='utf8') as f: for classify in f.readlines(): self.getShopCount(classify.strip('\n')) def getShopCount(self, q): # 店铺总数限制1000家 kw = { 'limit': '1000', 'uuid': 'd9b1d1075eae445b9941.1520753826.1.0.0', 'q': q } response = requests.get(self.searchUrl, params=kw, headers=self.headers) json_response = response.content.decode(encoding='utf-8') dict_json = json.loads(json_response) self.saveShopTmp(dict_json, q) # print response.url # print dict_json['data']['totalCount'] def saveShopTmp(self, dict_json, kw): for info in dict_json['data']['searchResult']: # self.saveData(info) # print info['title'].encode('utf-8').split('(')[0] self.saveShopCount(info, kw) def saveShopCount(self, info, kw): name = info['title'] address = info['address'] avgprice = info['avgprice'] avgscore = info['avgscore'] latitude = info['latitude'] longitude = info['longitude'] comments = info['comments'] try: sql = """ INSERT INTO nn_search(name,category, avgscore, latitude, longitude,avgprice,comments, address) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')""" % ( name, kw,avgscore, latitude, longitude,avgprice,comments, address) # 使用 execute() 方法执行 SQL 查询 self.cursor.execute(sql) print('存储成功') except Exception: print(traceback.print_exc()) finally: self.db.commit() if __name__ == '__main__': s = splider() # s.getShopCount(u'肯德基') s.getAllShopCount()