# Decompiled from: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
# Embedded file name: LngLatObtain.py
#coding=gbk
import tkinter
from tkinter import ttk, messagebox
from tkinter import *
from urllib import parse
import threading, time
from openpyxl import Workbook
import urllib.request
from bs4 import BeautifulSoup
from my_xlsx import *
from sheng import *
from GCJ02ToWGS84 import *
from gaodeyanzheng import *
from concurrent.futures import ThreadPoolExecutor, as_completed
Xlsnum = 1
wb = Workbook()
ws = wb.active
ws.append(["'id'", "'地市'", "'大类场景'", "'中类场景'", "'小类场景'", "'市'", "'区县'", "'名称'", "'location'", "'mergeLocation'"])
headerss = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
ids = []
def XmlList(types, city, key, Keyword, page=0):
global ids
if page == 0:
try:
page = 1
children = '1'
offset = '50'
extensions = 'all'
url = 'http://restapi.amap.com/v3/place/text?key=' + str(key).replace(' ', '') + '&keywords=' + parse.quote(Keyword) + '&types=' + types + '&city=' + city + '&children=' + children + '&offset=' + offset + '&page=' + str(page) + '&extensions=' + extensions + '&output=xml'
req = urllib.request.Request(url, headers=headers)
res = urllib.request.urlopen(req)
res = res.read()
soup = BeautifulSoup(res, 'html.parser')
cou = int(soup.response.count.get_text())
return math.ceil(int(soup.response.count.get_text()) / 50)
except Exception as e:
try:
print('print:' + types + ':' + city + ':' + key)
print(e)
XmlList(types, city, key, Keyword=Keyword, page=0)
finally:
e = None
del e
else:
try:
children = '1'
offset = '50'
extensions = 'all'
url = 'http://restapi.amap.com/v3/place/text?key=' + str(key).replace(' ', '') + '&keywords=' + parse.quote(Keyword) + '&types=' + types + '&city=' + city + '&children=' + children + '&offset=' + offset + '&page=' + str(page) + '&extensions=' + extensions + '&output=xml'
req = urllib.request.Request(url, headers=headers)
res = urllib.request.urlopen(req)
res = res.read()
soup = BeautifulSoup(res, 'html.parser')
xmls = soup.response.pois
for xml in xmls:
ids.append(xml.id.get_text())
except:
XmlList(types, city, key, page)
def LongitudeAndLatitude(ids, Scenario, city, area, second=0):
global Xlsnum
if second == 2:
pass
try:
url = 'http://m.amap.com/search/mapview/poiid=' + ids + ''
req = urllib.request.Request(url, headers=headerss)
res = urllib.request.urlopen(req, timeout=10)
res = res.read()
soup = BeautifulSoup(res, 'html.parser')
SSR_DATA = soup.html.script.get_text()
if 'window.__SSR_DATA__ = ' in str(SSR_DATA):
values = str(SSR_DATA).replace('window.__SSR_DATA__ = ', '').replace('true', "''").replace('false', "''").replace(';', '')
dics = eval(values)
location = dics['poi_list'][0]['longitude'] + ',' + dics['poi_list'][0]['latitude']
Scenename = dics['poi_list'][0]['disp_name']
mergeLocation = ''
try:
mergeLocation = dics['poi_list'][0]['domain_list'][len(dics['poi_list'][0]['domain_list']) - 1]['value']
except:
mergeLocation = ''
Areaname = dics['poi_list'][0]['cityname']
print(Scenename)
print(Xlsnum)
Preservation(ids, Areaname, Scenario, city, area, Scenename, location, mergeLocation)
Xlsnum += 1
else:
if str(soup.html.title.get_text()) == '高德验证':
h = HuXiu()
if h.visit_index() == '验证成功':
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
print('高德地图验证失败重新请求')
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
print('返回文件有误重新请求')
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
except Exception as e:
try:
if 'list index out of range' == e.args[0]:
print(soup)
else:
if 'value' == e.args[0]:
print(soup)
else:
if '[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。' == str(e.args[0]):
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
if 'timed out' == str(e.args[0]):
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
if 'The read operation timed out' == str(e.args[0]):
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
if '_ssl.c:1034: The handshake operation timed out' == str(e.args[0]):
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
else:
print(e.args[0])
finally:
e = None
del e
except error.URLError as ee:
try:
print(ee)
print('进入url异常')
LongitudeAndLatitude(ids, Scenario, city, area, second + 1)
finally:
ee = None
del ee
mutex = threading.Lock()
def Preservation(ids, Areaname, Scenario, city, area, Scenename, location, boundary):
mutex.acquire()
scene = str(Scenario).split('_')
ws.append([ids, Areaname, scene[0], scene[1], scene[2], city, area, Scenename, str(my_local(str(location).replace(' ', ''))), '{' + hang(str(boundary).replace('_', ';')) + '}'])
wb.save(city + '_' + scene[0] + '.xlsx')
mutex.release()
def Click_1():
if CheckbuttonL8state.get() == 1:
CheckbuttonL9['state'] = DISABLED
comboxlist3['state'] = DISABLED
comboxlist4['state'] = DISABLED
else:
CheckbuttonL9['state'] = 'normal'
comboxlist3['state'] = 'normal'
comboxlist4['state'] = 'normal'
def Click_2():
if CheckbuttonL9state.get() == 1:
CheckbuttonL8['state'] = DISABLED
comboxlist4['state'] = DISABLED
else:
CheckbuttonL8['state'] = 'normal'
comboxlist4['state'] = 'normal'
def Thread_It(func, *args):
"""将函数打包进线程"""
t = threading.Thread(target=func, args=args)
t.setDaemon(True)
t.start()
def ShengKey(*args):
if len(sheng_value(comboxlist.get())) == 1:
comboxlist11['values'] = sheng_shi(sheng_value(comboxlist.get())[0])
comboxlist11.current(0)
ShiKey()
comboxlist['values'] = sheng_value(comboxlist.get())
else:
comboxlist['values'] = sheng_value(comboxlist.get())
def ShiKey(*args):
if len(shi_value(comboxlist11.get())) == 1:
comboxlist12['values'] = shi_qu(shi_value(comboxlist11.get())[0])
comboxlist12.current(0)
else:
comboxlist11['values'] = shi_value(comboxlist2.get())
def DaLeiKey(*args):
if len(dalei_value(comboxlist2.get())) == 1:
comboxlist3['values'] = dalei_zhonglei(dalei_value(comboxlist2.get())[0])
comboxlist3.current(0)
ZhongLeiKey()
else:
comboxlist2['values'] = dalei_value(comboxlist2.get())
def ZhongLeiKey(*args):
if len(zhonglei_value(comboxlist3.get())) == 1:
comboxlist4['values'] = zhonglei_xiaolei(zhonglei_value(comboxlist3.get())[0])
comboxlist4.current(0)
else:
comboxlist3['values'] = dalei_zhonglei(comboxlist3.get())
def Inlet(*args):
if entry.get() == '':
tkinter.messagebox.showinfo('提示', 'key没有输入')
else:
mains()
print('高德地图边界采集 1.0')
def mains():
shenglist = sheng_shi(comboxlist12.get())
shilist = shi_qu(comboxlist12.get())
poexecutor = ThreadPoolExecutor(10)
if len(shenglist) > 0:
for a in shenglist:
for b in shi_qu(a):
if b == comboxlist.get():
continue
else:
if b == a:
continue
if CheckbuttonL8state.get() == 1:
zhongleilist = dalei_zhonglei(comboxlist2.get())
for zhongleiname in zhongleilist:
xiaoleilist = zhonglei_xiaolei(zhongleiname)
for xiaoleiname in xiaoleilist:
types = typeid(xiaoleiname)
text.insert(INSERT, str(b) + '\n')
shengid = qu_id(comboxlist.get() + '_' + a + '_' + b)
city = shengid
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), str(a), str(b)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
if CheckbuttonL8state.get() == 0:
if CheckbuttonL9state.get() == 1:
xiaoleilist = zhonglei_xiaolei(comboxlist3.get())
for xiaoleiname in xiaoleilist:
types = typeid(xiaoleiname)
text.insert(INSERT, str(b) + '\n')
shengid = qu_id(comboxlist.get() + '_' + a + '_' + b)
city = shengid
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), str(a), str(b)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
else:
text.insert(INSERT, str(b) + '\n')
shengid = qu_id(comboxlist.get() + '_' + a + '_' + b)
types = typeid(comboxlist4.get())
city = shengid
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(comboxlist4.get()) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), str(a), str(b)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
tkinter.messagebox.showinfo('提示', '爬取完毕')
else:
if len(shilist) > 1:
for a in shilist:
if len(shi_qu(a)) > 0:
continue
if a == comboxlist.get():
continue
if CheckbuttonL8state.get() == 1:
zhongleilist = dalei_zhonglei(comboxlist2.get())
for zhongleiname in zhongleilist:
xiaoleilist = zhonglei_xiaolei(zhongleiname)
for xiaoleiname in xiaoleilist:
text.insert(INSERT, str(a) + '\n')
shengid = qu_id(comboxlist.get() + '_' + comboxlist12.get() + '_' + a)
city = shengid
types = typeid(xiaoleiname)
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), str(a)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
elif CheckbuttonL8state.get() == 0:
if CheckbuttonL9state.get() == 1:
xiaoleilist = zhonglei_xiaolei(comboxlist3.get())
for xiaoleiname in xiaoleilist:
text.insert(INSERT, str(a) + '\n')
shengid = qu_id(comboxlist.get() + '_' + comboxlist12.get() + '_' + a)
city = shengid
types = typeid(xiaoleiname)
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), str(a)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
else:
text.insert(INSERT, str(a) + '\n')
shengid = qu_id(comboxlist.get() + '_' + comboxlist12.get() + '_' + a)
city = shengid
types = typeid(comboxlist4.get())
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(comboxlist4.get()) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), str(a)) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
tkinter.messagebox.showinfo('提示', '爬取完毕')
else:
if CheckbuttonL8state.get() == 1:
zhongleilist = dalei_zhonglei(comboxlist2.get())
for zhongleiname in zhongleilist:
xiaoleilist = zhonglei_xiaolei(zhongleiname)
for xiaoleiname in xiaoleilist:
city = qu_id(comboxlist.get() + '_' + comboxlist11.get() + '_' + comboxlist12.get())
types = typeid(xiaoleiname)
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), comboxlist12.get()) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
tkinter.messagebox.showinfo('提示', '爬取完毕')
else:
if CheckbuttonL8state.get() == 0:
if CheckbuttonL9state.get() == 1:
xiaoleilist = zhonglei_xiaolei(comboxlist3.get())
for xiaoleiname in xiaoleilist:
city = qu_id(comboxlist.get() + '_' + comboxlist11.get() + '_' + comboxlist12.get())
types = typeid(xiaoleiname)
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(xiaoleiname) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), comboxlist12.get()) for id in ids]
for future in as_completed(all_task):
data = future.result()
del ids[:]
tkinter.messagebox.showinfo('提示', '爬取完毕')
else:
city = qu_id(comboxlist.get() + '_' + comboxlist11.get() + '_' + comboxlist12.get())
types = typeid(comboxlist4.get())
xhuan = XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()))
for x in range(1, int(xhuan) + 1):
XmlList((str(types)), (str(city)), (entry.get()), Keyword=(Keyword.get()), page=x)
text.insert(INSERT, '共:' + str(len(ids)) + '\n')
text.insert(INSERT, str(comboxlist4.get()) + '\n')
all_task = [poexecutor.submit(LongitudeAndLatitude, id, comboxlist2.get() + '_' + comboxlist3.get() + '_' + comboxlist4.get(), comboxlist11.get(), comboxlist12.get()) for id in ids]
for future in as_completed(all_task):
data = future.result()
tkinter.messagebox.showinfo('提示', '爬取完毕')
del ids[:]
tkinter.messagebox.showinfo('提示', '爬取完毕')
root = tkinter.Tk()
root.title('地图边界获取')
root.geometry('640x500')
root.resizable(width=True, height=False)
var = tkinter.Variable()
CheckbuttonL8state = tkinter.IntVar()
CheckbuttonL9state = tkinter.IntVar()
Initialization()
Initialization_dishi()
Ll1 = Label(root, text='本程序主要用于从高德地图上爬取地理边界。关键字段的选择可手动编写,也可').place(x=20, y=10, anchor='nw')
Ll2 = Label(root, text='通过下拉框选择;高德地图key是在高德API网站申请开发者账号时产生的。').place(x=5, y=30, anchor='nw')
L7 = Label(root, text='关键字:').place(x=10, y=80, anchor='nw')
Keyword = Entry(root, font='微软雅黑', width=60)
Keyword.place(x=60, y=80, anchor='nw')
L1 = Label(root, text='地区:').place(x=10, y=110, anchor='nw')
comvalue = tkinter.StringVar()
comboxlist = ttk.Combobox(root, textvariable=comvalue)
comboxlist['values'] = Initialization_sheng()
comboxlist.current(0)
comboxlist.bind('>', ShengKey)
comboxlist.bind('', ShengKey)
comboxlist.place(x=60, y=110, anchor='nw')
comvalue11 = tkinter.StringVar()
comboxlist11 = ttk.Combobox(root, textvariable=comvalue11)
comboxlist11['values'] = '请选择上一级'
comboxlist11.current(0)
comboxlist11.bind('>', ShiKey)
comboxlist11.bind('', ShiKey)
comboxlist11.place(x=250, y=110, anchor='nw')
comvalue12 = tkinter.StringVar()
comboxlist12 = ttk.Combobox(root, textvariable=comvalue12)
comboxlist12['values'] = shenglei('')
comboxlist12.current(0)
comboxlist12.bind('>')
comboxlist12.bind('')
comboxlist12.place(x=440, y=110, anchor='nw')
L2 = Label(root, text='大类:').place(x=10, y=140, anchor='nw')
comvalue2 = tkinter.StringVar()
comboxlist2 = ttk.Combobox(root, textvariable=comvalue2)
comboxlist2['values'] = Initializationdalei()
comboxlist2.current(0)
comboxlist2.bind('>', DaLeiKey)
comboxlist2.bind('', DaLeiKey)
comboxlist2.place(x=60, y=140, anchor='nw')
L8 = Label(root, text='大类下按中类循环爬取:').place(x=250, y=140, anchor='nw')
CheckbuttonL8 = Checkbutton(root, variable=CheckbuttonL8state, command=Click_1)
CheckbuttonL8.place(x=390, y=140, anchor='nw')
L3 = Label(root, text='中类:').place(x=10, y=170, anchor='nw')
comvalue3 = tkinter.StringVar()
comboxlist3 = ttk.Combobox(root, textvariable=comvalue3)
comboxlist3['values'] = '请选择上一级'
comboxlist3.current(0)
comboxlist3.bind('>', ZhongLeiKey)
comboxlist3.bind('', ZhongLeiKey)
comboxlist3.place(x=60, y=170, anchor='nw')
L9 = Label(root, text='中类下按小类循环爬取:').place(x=250, y=170, anchor='nw')
CheckbuttonL9 = Checkbutton(root, variable=CheckbuttonL9state, command=Click_2)
CheckbuttonL9.place(x=390, y=170, anchor='nw')
L4 = Label(root, text='小类:').place(x=10, y=200, anchor='nw')
comvalue4 = tkinter.StringVar()
comboxlist4 = ttk.Combobox(root, textvariable=comvalue4)
comboxlist4['values'] = '请选择上一级'
comboxlist4.current(0)
comboxlist4.place(x=60, y=200, anchor='nw')
L5 = Label(root, text='高德地图key:').place(x=10, y=230, anchor='nw')
entry = Entry(root, font='微软雅黑', width=30)
entry.place(x=100, y=230, anchor='nw')
button = Button(root, text='开始下载', width=10, font=('微软雅黑', 10), command=(lambda: Thread_It(Inlet)))
button.place(x=190, y=260, anchor='nw')
L6 = Label(root, text='输出:').place(x=10, y=290, anchor='nw')
text = Text(root, font=('微软雅黑', 16), width=40, height=5)
text.place(x=10, y=330, anchor='nw')
root.mainloop()
# okay decompiling LngLatObtain.pyc