FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonStudy/google_api/baidu_api.py at master · gitstudy8/pythonStudy · GitHub
gitstudy8
pythonStudy
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonStudy
/
google_api
/
baidu_api.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (84 loc) · 2.74 KB
Breadcrumbs
pythonStudy
/
google_api
/
baidu_api.py
Copy path
File metadata and controls
96 lines (84 loc) · 2.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# coding:utf-8
import
xlrd
import
xlwt
import
requests
import
urllib
import
math
import
re
import
chardet
pattern_x
=
re
.
compile
(
r'"x":(".+?")'
)
pattern_y
=
re
.
compile
(
r'"y":(".+?")'
)
# 墨卡托转经纬度
# http://www.cnblogs.com/liongis/archive/2012/06/13/2548061.html
def
mercator2wgs84
(
mercator
):
# key1=mercator.keys()[0]
# key2=mercator.keys()[1]
point_x
=
mercator
[
0
]
point_y
=
mercator
[
1
]
print
'point_x'
,
point_x
,
point_y
x
=
point_x
/
20037508.3427892
*
180
y
=
point_y
/
20037508.3427892
*
180
y
=
180
/
math
.
pi
*
(
2
*
math
.
atan
(
math
.
exp
(
y
*
math
.
pi
/
180
))
-
math
.
pi
/
2
)
print
'x:'
,
x
,
'y:'
,
y
return
(
x
,
y
)
def
get_mercator
(
addr
):
# print '------' + addr
quote_addr
=
urllib
.
quote
(
addr
.
encode
(
'utf8'
))
city
=
urllib
.
quote
(
u'齐齐哈尔市龙'
.
encode
(
'utf8'
))
province
=
urllib
.
quote
(
u'黑龙江省'
.
encode
(
'utf8'
))
if
quote_addr
.
startswith
(
city
)
or
quote_addr
.
startswith
(
province
):
pass
else
:
# print 'quote_addr1',quote_addr
# print 'type:', type(quote_addr)
# print 'type1:',chardet.detect(quote_addr)
# # quote_addr:<type 'str'>---ascii
# aaa=unicode(quote_addr, 'gbk')
# # print 'type2:',chardet.detect(aaa)
# print 'aaaa:'+urllib.unquote(quote_addr)
# ## 成功还原回去
# quote_addr = city + quote_addr
print
'quote_addr2'
,
urllib
.
unquote
(
quote_addr
)
s
=
urllib
.
quote
(
u'北京市'
.
encode
(
'utf8'
))
api_addr
=
"http://api.map.baidu.com/?qt=gc&wd=%s&cn=%s&ie=utf-8&oue=1&fromproduct=jsapi&res=api&callback=BMap._rd._cbk62300"
%
(
quote_addr
,
s
)
req
=
requests
.
get
(
api_addr
)
print
'api_addr'
,
api_addr
content
=
req
.
content
.
encode
(
'utf8'
)
print
'--1111-----------------'
print
content
print
'--2222---------------------'
x
=
re
.
findall
(
pattern_x
,
content
)
y
=
re
.
findall
(
pattern_y
,
content
)
if
x
:
x
=
x
[
0
]
y
=
y
[
0
]
x
=
x
[
1
:
-
1
]
y
=
y
[
1
:
-
1
]
x
=
float
(
x
)
y
=
float
(
y
)
location
=
(
x
,
y
)
else
:
location
=
()
return
location
def
run
():
data
=
xlrd
.
open_workbook
(
'Book2.xlsx'
)
rtable
=
data
.
sheets
()[
0
]
nrows
=
rtable
.
nrows
values
=
rtable
.
col_values
(
0
)
workbook
=
xlwt
.
Workbook
()
wtable
=
workbook
.
add_sheet
(
'data'
,
cell_overwrite_ok
=
True
)
row
=
0
for
value
in
values
:
mercator
=
get_mercator
(
value
)
if
mercator
:
wgs
=
mercator2wgs84
(
mercator
)
else
:
wgs
=
(
'NotFound'
,
'NotFound'
)
print
"%s,%s,%s"
%
(
value
,
wgs
[
0
],
wgs
[
1
])
wtable
.
write
(
row
,
0
,
value
)
wtable
.
write
(
row
,
1
,
wgs
[
0
])
wtable
.
write
(
row
,
2
,
wgs
[
1
])
row
=
row
+
1
workbook
.
save
(
'data.xls'
)
if
__name__
==
'__main__'
:
run
()
Back
|
FazBrowse Home
|
New Git URL