#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Contact : liuyuqi.gov@msn.cn @Time : 2023/05/29 19:20:39 @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved. @Desc : 手机接口 ''' import hashlib import base64,json,random,time import requests class KePhone(object): ''' 调用app接口 ''' def __init__(self): pass # 获取挂牌信息-贝壳网信息 def get_guapai_data(self, xiaoqu_id): guapai_url = 'https://app.api.ke.com/house/ershoufang/searchv5' guapai_data = { 'fullFilters': '1', 'containerType': '2', 'limitCount': '20', 'condition': xiaoqu_id, 'cityId': '**********', 'limitOffset': '0' } # header抓一下复制进来 headers = { "x-req-id": "**********", "Page-Schema": "ershou%2Flist", "Referer": "ershoulistsearch", "Cookie": "lianjia_udid=********;" "lianjia_ssid=**********;" "lianjia_uuid=**********", "Lianjia-City-Id": "**********", "User-Agent": "Beike2.31.0;Android MuMu; Android 6.0.1", "Lianjia-Channel": "**********", "Lianjia-Device-Id": "**********", "Lianjia-Version": "2.31.0", "Lianjia-Im-Version": "2.34.0", "Lianjia-Recommend-Allowable": "1", "Authorization": self.generateAuthorization(guapai_url, guapai_data), "ip": "**********", "wifi_name": "**********", "lat": "**********", "lng": "**********", "Host": "app.api.ke.com", "Connection": "Keep-Alive", "Accept-Encoding": "gzip" } guapai_res = requests.get(guapai_url, headers=headers, params=guapai_data) # 获取小区的挂牌信息列表 guapai_json = json.loads(guapai_res.text) if guapai_json['errno'] == 0: guapai_info = guapai_json['data']['list'] for i in guapai_info: # 插入数据库的信息列表 sql_data = [] if 'houseCode' in i: # house_code house_code = i['houseCode'] sql_data.append(house_code) # 标题 resblock_name = i['title'] sql_data.append(resblock_name) # 描述 resblock_desc = i['desc'] sql_data.append(resblock_desc) # 总价 total_p = i['priceStr'] total_p = self.return_no(total_p) sql_data.append(total_p) # 小区 communityName = i['communityName'] sql_data.append(communityName) # basicList处理 for basic_info in i['basicList']: # pass sql_data.append(basic_info['value']) # infoList处理 for infoList_info in i['infoList']: infoList_data = infoList_info['value'] if infoList_info['name'] == '单价:': # 返回数字 infoList_data = self.return_no(infoList_data) sql_data.append(infoList_data) # 构造URL,用于微信推送 url = 'https://m.ke.com/tj/ershoufang/' + house_code + '.html' sql_data.append(url) # 数据插入数据库 self.insert_guapai(sql_data) # 休眠 sleeptime = random.randint(2, 10) time.sleep(sleeptime) # 获取Authorization # 来源https://github.com/ShiJianYingxiang/origin/blob/master/fang_beike/fang_beike/spiders/ershou_viewer.py # url没用 def generateAuthorization(self, url, url_parm): secret_key = "d5e343d453aecca8b14b2dc687c381ca" secret_id = "20180111_android" # 提取URL内参数 # url_parm = {i.split("=")[0]: i.split("=")[1] for i in url.split("?")[1].split("&")} # 参数排序 url_parm_sort = sorted(url_parm.items(), key=lambda x: x[0], reverse=False) p2 = secret_key + "".join([i[0] + "=" + i[1] for i in url_parm_sort]) v3 = hashlib.sha1(p2.encode('utf-8')).hexdigest() v4 = secret_id + ":" + v3 v5 = base64.b64encode(v4.encode("utf-8")) return v5.decode() if __name__ == "__main__": pass