phone.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/05/29 19:20:39
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : 手机接口
  8. '''
  9. import hashlib
  10. import base64,json,random,time
  11. import requests
  12. class KePhone(object):
  13. ''' 调用app接口 '''
  14. def __init__(self):
  15. pass
  16. # 获取挂牌信息-贝壳网信息
  17. def get_guapai_data(self, xiaoqu_id):
  18. guapai_url = 'https://app.api.ke.com/house/ershoufang/searchv5'
  19. guapai_data = {
  20. 'fullFilters': '1',
  21. 'containerType': '2',
  22. 'limitCount': '20',
  23. 'condition': xiaoqu_id,
  24. 'cityId': '**********',
  25. 'limitOffset': '0'
  26. }
  27. # header抓一下复制进来
  28. headers = {
  29. "x-req-id": "**********",
  30. "Page-Schema": "ershou%2Flist",
  31. "Referer": "ershoulistsearch",
  32. "Cookie": "lianjia_udid=********;"
  33. "lianjia_ssid=**********;"
  34. "lianjia_uuid=**********",
  35. "Lianjia-City-Id": "**********",
  36. "User-Agent": "Beike2.31.0;Android MuMu; Android 6.0.1",
  37. "Lianjia-Channel": "**********",
  38. "Lianjia-Device-Id": "**********",
  39. "Lianjia-Version": "2.31.0",
  40. "Lianjia-Im-Version": "2.34.0",
  41. "Lianjia-Recommend-Allowable": "1",
  42. "Authorization": self.generateAuthorization(guapai_url, guapai_data),
  43. "ip": "**********",
  44. "wifi_name": "**********",
  45. "lat": "**********",
  46. "lng": "**********",
  47. "Host": "app.api.ke.com",
  48. "Connection": "Keep-Alive",
  49. "Accept-Encoding": "gzip"
  50. }
  51. guapai_res = requests.get(guapai_url, headers=headers, params=guapai_data)
  52. # 获取小区的挂牌信息列表
  53. guapai_json = json.loads(guapai_res.text)
  54. if guapai_json['errno'] == 0:
  55. guapai_info = guapai_json['data']['list']
  56. for i in guapai_info:
  57. # 插入数据库的信息列表
  58. sql_data = []
  59. if 'houseCode' in i:
  60. # house_code
  61. house_code = i['houseCode']
  62. sql_data.append(house_code)
  63. # 标题
  64. resblock_name = i['title']
  65. sql_data.append(resblock_name)
  66. # 描述
  67. resblock_desc = i['desc']
  68. sql_data.append(resblock_desc)
  69. # 总价
  70. total_p = i['priceStr']
  71. total_p = self.return_no(total_p)
  72. sql_data.append(total_p)
  73. # 小区
  74. communityName = i['communityName']
  75. sql_data.append(communityName)
  76. # basicList处理
  77. for basic_info in i['basicList']:
  78. # pass
  79. sql_data.append(basic_info['value'])
  80. # infoList处理
  81. for infoList_info in i['infoList']:
  82. infoList_data = infoList_info['value']
  83. if infoList_info['name'] == '单价:':
  84. # 返回数字
  85. infoList_data = self.return_no(infoList_data)
  86. sql_data.append(infoList_data)
  87. # 构造URL,用于微信推送
  88. url = 'https://m.ke.com/tj/ershoufang/' + house_code + '.html'
  89. sql_data.append(url)
  90. # 数据插入数据库
  91. self.insert_guapai(sql_data)
  92. # 休眠
  93. sleeptime = random.randint(2, 10)
  94. time.sleep(sleeptime)
  95. # 获取Authorization
  96. # 来源https://github.com/ShiJianYingxiang/origin/blob/master/fang_beike/fang_beike/spiders/ershou_viewer.py
  97. # url没用
  98. def generateAuthorization(self, url, url_parm):
  99. secret_key = "d5e343d453aecca8b14b2dc687c381ca"
  100. secret_id = "20180111_android"
  101. # 提取URL内参数
  102. # url_parm = {i.split("=")[0]: i.split("=")[1] for i in url.split("?")[1].split("&")}
  103. # 参数排序
  104. url_parm_sort = sorted(url_parm.items(), key=lambda x: x[0], reverse=False)
  105. p2 = secret_key + "".join([i[0] + "=" + i[1] for i in url_parm_sort])
  106. v3 = hashlib.sha1(p2.encode('utf-8')).hexdigest()
  107. v4 = secret_id + ":" + v3
  108. v5 = base64.b64encode(v4.encode("utf-8"))
  109. return v5.decode()
  110. if __name__ == "__main__":
  111. pass