getBaiduMap.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2019/11/18 03:20:34
  7. @Version : 1.0
  8. @License : (C)Copyright 2019
  9. @Desc : 百度地图信息采集
  10. '''
  11. import requests
  12. import os
  13. import re
  14. import json
  15. from bs4 import BeautifulSoup
  16. class BaiduMap(object):
  17. """docstring for BaiduMap"""
  18. def __init__(self):
  19. super(BaiduMap, self).__init__()
  20. #城市获取数据
  21. def getCityData(self,cityName):
  22. # http://map.baidu.com/?newmap=1&qt=cur&ie=utf-8&wd= &oue=1&res=jc
  23. try:
  24. webData = requests.get("http://map.baidu.com/?newmap=1&qt=cur&ie=utf-8&wd=" + cityName + "&oue=1&res=jc").text
  25. jsonData = json.loads(webData)
  26. print(jsonData,end="\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
  27. if 'weather' in jsonData: #存在天气预报的情况下
  28. weatherData = json.loads(jsonData['weather'])
  29. print(weatherData['OriginQuery']," PM2.5:",weatherData['pm25'],weatherData['weather0'],"[",weatherData['temp0'],"][",weatherData['wind0'],"]",end=' ')
  30. if 'cur_area_id' in jsonData:
  31. print("城市id:",jsonData['cur_area_id'])
  32. return jsonData['cur_area_id']
  33. else:
  34. return -1
  35. except Exception as e:
  36. raise
  37. def getMapData(self,cityId,info_):
  38. qt = "s"
  39. rn = "10"
  40. modNum = "10"
  41. loopValue = 1
  42. if cityId < 0 :
  43. return -1
  44. getUrl = "http://api.map.baidu.com/?qt=" + qt + "&c=" + str(cityId) + "&wd=" + info_ + "&rn=" + rn + "&pn=1" + "&ie=utf-8&oue=1&fromproduct=jsapi&res=api&callback=BMap._rd._cbk7303&ak=E4805d16520de693a3fe707cdc962045";
  45. webData = requests.get(getUrl).text
  46. # print(webData)
  47. loopNum = re.search("\"total\":([\\s\\S]*?),",webData).group(1) #数量
  48. reJson = re.search("content\":([\\s\\S]*?),\"current_city",webData).group(1)
  49. print(loopNum)
  50. jsonData = json.loads(reJson)
  51. print(jsonData)
  52. if __name__ == '__main__':
  53. obj = BaiduMap()
  54. obj.getMapData(obj.getCityData("潮州"),"酒店")