回答編集履歴
2
コードの編集
answer
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
base_url = 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&address={}&key=***'
|
16
16
|
headers = {'content-type': 'application/json'}
|
17
17
|
|
18
|
-
queries = [
|
18
|
+
queries = []
|
19
19
|
with open(r'C:\work\test.txt', 'r') as f:
|
20
20
|
queries = f.readlines()
|
21
21
|
for query in queries:
|
1
コードの追記
answer
CHANGED
@@ -1,2 +1,34 @@
|
|
1
1
|
入力ファイルのC:\work\test.txtがSJISになっていませんか?
|
2
|
-
UTF-8でファイルを作成したところ動作しました。
|
2
|
+
UTF-8でファイルを作成したところ動作しました。
|
3
|
+
|
4
|
+
2019/02/19 8:01追記
|
5
|
+
ファイル出力にしたコードを載せておきます。
|
6
|
+
URLのアスタリスクは実際には有効なAPIキーが入ります。
|
7
|
+
```Python
|
8
|
+
# -*- coding: utf-8 -*-
|
9
|
+
|
10
|
+
import requests
|
11
|
+
import json
|
12
|
+
from time import sleep
|
13
|
+
|
14
|
+
wait_time = 0.3 # (sec)
|
15
|
+
base_url = 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&address={}&key=***'
|
16
|
+
headers = {'content-type': 'application/json'}
|
17
|
+
|
18
|
+
queries = ['赤羽駅']
|
19
|
+
with open(r'C:\work\test.txt', 'r') as f:
|
20
|
+
queries = f.readlines()
|
21
|
+
for query in queries:
|
22
|
+
query = query.strip()
|
23
|
+
url = base_url.format(query)
|
24
|
+
r = requests.get(url, headers=headers)
|
25
|
+
data = r.json()
|
26
|
+
if 'results' in data and len(data['results']) > 0 and 'formatted_address' in data['results'][0]:
|
27
|
+
with open(r'C:\work\test_out.txt', 'a') as out:
|
28
|
+
out.write('{}@{}@{}@{}'.format(
|
29
|
+
query,
|
30
|
+
data['results'][0]['formatted_address'].encode('utf-8'),
|
31
|
+
data['results'][0]['geometry']['location']['lat'],
|
32
|
+
data['results'][0]['geometry']['location']['lng']
|
33
|
+
))
|
34
|
+
```
|