class Hot_City_Coordinates():
"""获取热门城市"""
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
'Cookie': 'BIDUPSID=708E39B6F293AF8D7B18BD51D6E15C42; PSTM=1596811009; BAIDUID=708E39B6F293AF8D297129216C3D6397:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; MCITY=-%3A; delPer=0; PSINO=5; H_PS_PSSID=32294_1456_32300_32380_32327_31660_32351_32046_32398_32116_32525_32482_22160',
}
self.content_list = [] # 存储市数据
self.content_list_ui = [] # 存储UI界面数据
def initialize(self, path_):
"""
初始化目录
"""
try:
if os.path.exists(path_): # 目录存在,返回为真
return path_
else:
os.mkdir(path_)
return path_
except Exception as e:
print(e)
def hot_city(self,map_pro, content , _path):
"""获取热门城市"""
country_url = 'https://map.baidu.com/?newmap=1&reqflag=pcmap&biz=1&from=webmap&da_par=after_baidu&pcevaname=pc4.1&qt=s&da_src=searchBox.button&wd=' + str(
content) + '&c=1&src=0&wd2=&pn=0&sug=0&l=5&b=(8661331.22,1425243.879999999;14551379.22,7913307.879999999)&from=webmap&biz_forward={%22scaler%22:2,%22styles%22:%22pl%22}&sug_forward=&auth=vQPQf98AKyG0bHBMP8U5%3DyJ31HF0NUV5uxHTNVEERVRtxjhNwzWWvy1uVt1GgvPUDZYOYIZuVtcvY1SGpuEt2gz4yYxGccZcuVtPWv3GuBRtal73hJUvhgMZSguxzBEHLNRTVtcEWe1GD8zv7u%40ZPuzztgwBzvf0wd0vyIUO7AMSFOukoPPB16A2mmiJLL'
print(country_url)
response = requests.get(country_url, headers=self.headers, verify=False)
json_con = response.json()
# 获取所有市的坐标并进行切割拼接 市的坐标可以百度
split_result = Provinces_cities_coordinates.split('\n')
city_dict = {x.split(' ')[1]: x.split(' ')[0] for x in split_result[1:]}
print(city_dict)
mkdir_path = _path + '/' + map_pro + '-' + content
province_path = self.initialize(mkdir_path)
if map_pro == '热门城市':
for x in json_con['content']:
hot_city_code = x['code']
AUTH = json_con['result']['auth']
hot_city_geo = x['geo']
hot_city_name = x['name']
city_ur1 = f'https://map.baidu.com/?newmap=1&reqflag=pcmap&biz=1&from=webmap&da_par=after_baidu&pcevaname=pc4.1&qt=spot&from=webmap&c={str(hot_city_code)}&wd={content}&wd2=&pn=0&nn=0&db=0&sug=0&addr=0&&da_src=pcmappg.poi.page&on_gel=1&src=7&gr=3&l=5&rn=50&tn=B_NORMAL_MAP&auth={AUTH}&ie=utf-8&b=({hot_city_geo})'
print('url', city_ur1)
t = threading.Thread(target=self.get_city_data,
args=(city_ur1, hot_city_name, map_pro, province_path, content))
t.start()
def get_city_data(self, city_ur1, city_name, map_pro, _path, content):
"""获取市的数据"""
city_all = requests.get(city_ur1, headers=self.headers)
city_json = city_all.json()
# 获取数据每个市的数据
try:
for city in city_json['content']:
content_dict = {}
content_dict['middle_name'] = city['name'] # 数据名字
content_dict['middle_address'] = city['addr'] # 数据地址
try:
content_dict['middle_tel'] = city['tel'] # 数据电话
except Exception as e:
content_dict['middle_tel'] = '暂无电话'
self.content_list.append(content_dict)
self.content_list_ui.append(content_dict)
print(city_name, '开始存入')
# 调用保存数据
t = threading.Thread(target=self.get_save, args=(map_pro, city_name, content, _path))
t.start()
self.content_list.clear()
except Exception as e:
print(e)
# print("最后一次打印")
if __name__ == '__main__':
...
# map_pro = input('请输入查询的省份:')
# content = input('请输入查询的数据:')
pcc = Hot_City_Coordinates()
pcc.hot_city('热门城市', '大学', '存放的路径')