def get_all(city, typeh): if city not in settings.beikecityDict: raise Exception('没有找到该城市') url = settings.beikecityDict[city] totalDict = {} if typeh not in ['二手房', '新房', '租房']: raise Exception('没有找到类型') totalDict['name'] = 'beike' totalDict['city'] = city totalDict['type'] = typeh totalDict['pages'] = 100 totalList = [] for i in range(1, 101): lists = get_page(page=i, base_url=url, typeh=typeh) for item in lists: totalList.append(item) totalDict['data'] = totalList jsonText = json.dumps(totalDict, ensure_ascii=False) if (typeh == '二手房'): jsonText = transform.trans_seconde_house(jsonText) elif (typeh == '新房'): jsonText = transform.trans_newHouse(jsonText) elif (typeh == '租房'): jsonText = transform.trans_rent_house(jsonText) return jsonText
def run(city, typeh, page): if city not in settings.beikecityDict: raise Exception('没有找到该城市') url = settings.beikecityDict[city] if page < 1 or page > 100: raise Exception('输入错误或越界,请重新输入!') totalDict = {} if typeh not in ['二手房', '新房', '租房']: raise Exception('没有找到类型') totalDict['name'] = 'beike' totalDict['city'] = city totalDict['type'] = typeh totalDict['pages'] = 100 totalDict['data'] = get_page(page=page, base_url=url, typeh=typeh) jsonText = json.dumps(totalDict, ensure_ascii=False) # 统一格式 if (typeh == '二手房'): jsonText = transform.trans_seconde_house(jsonText) elif (typeh == '新房'): jsonText = transform.trans_newHouse(jsonText) elif (typeh == '租房'): jsonText = transform.trans_rent_house(jsonText) return jsonText