質問編集履歴

4

実証コードに変更

2017/10/30 22:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -38,9 +38,19 @@
38
38
 
39
39
 
40
40
 
41
+ ---
42
+
43
+
44
+
45
+ **2017-10-31 コード修正**
46
+
47
+ suyamaさんのアドバイスを受け、ページをクロールしながらタイトルを拾えるところまでできました。ここまでできればあとはwxPythonか何かでGUIをつければOKですね。ここで一区切りにします。Pythonで検索しようとしている人の参考までにコードを載せます。
48
+
49
+
50
+
41
51
  ```Python
42
52
 
43
- # coding: UTF-8
53
+ # coding: UTF-8
44
54
 
45
55
  import os
46
56
 
@@ -49,6 +59,8 @@
49
59
  import json
50
60
 
51
61
  import ssl
62
+
63
+ import sys
52
64
 
53
65
 
54
66
 
@@ -66,7 +78,7 @@
66
78
 
67
79
 
68
80
 
69
- URL = "https://teratail.com/api/v1/users/" + USER_NAME + "/clips"
81
+ URL = "https://teratail.com/api/v1/users/" + USER_NAME + "/clips?limit=100&page="
70
82
 
71
83
 
72
84
 
@@ -74,31 +86,35 @@
74
86
 
75
87
 
76
88
 
77
- def downloader():
89
+ def downloader(page):
78
90
 
79
- # Get clip data from teratail
91
+ try:
80
92
 
81
- with urllib.request.urlopen(URL) as response:
93
+ # Get clip data from teratail
82
94
 
83
- html = response.read()
95
+ with urllib.request.urlopen(URL + str(page)) as response:
84
96
 
97
+ html = response.read()
98
+
85
- text = html.decode('utf-8')
99
+ text = html.decode('utf-8')
86
100
 
87
101
 
88
102
 
89
- # Load clip data as json file
103
+ # Load clip data as json file
90
104
 
91
- json_dat = json.loads(text)
105
+ json_dat = json.loads(text)
92
106
 
93
- # print(json.dumps(json_dat, sort_keys = True, indent = 4)
107
+ # print(json.dumps(json_dat, sort_keys = True, indent = 4)
94
108
 
95
- # return json_dat["meta"]
109
+
96
110
 
111
+ # Return clip data as dict type
97
112
 
113
+ return json_dat["meta"], json_dat["questions"]
98
114
 
99
- # Return clip data as dict type
115
+ except:
100
116
 
101
- return json_dat["questions"]
117
+ sys.exit("The process was terminated. You may got 403 error caused by excess access [30 access / hour * IP address].")
102
118
 
103
119
 
104
120
 
@@ -108,12 +124,32 @@
108
124
 
109
125
  for dct in lst_questions:
110
126
 
111
- print( dct["created"] + dct["title"])
127
+ # print( dct["created"] + " " + dct["title"])
128
+
129
+ print(dct["title"])
112
130
 
113
131
 
114
132
 
115
133
  if __name__ == "__main__":
116
134
 
135
+ # Initialize page index
136
+
137
+ page = 1
138
+
139
+ page_max = 1
140
+
141
+
142
+
143
+ # Crawl each pages
144
+
145
+ while page <= page_max:
146
+
147
+ meta, title = downloader(page)
148
+
149
+ page_max = meta["total_page"]
150
+
117
- crop_titles(downloader())
151
+ crop_titles(title)
152
+
153
+ page = page + 1
118
154
 
119
155
  ```

3

誤植修正

2017/10/30 22:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  - **1)**
30
30
 
31
- 検証時にChromeで[URL](https://teratail.com/api/v1/users/slash/)を直接開くと問題はなかったのですが、Pythonでそのまま走らせるとSSLの認証エラーではじかれました。なので、'ssl._create_default_https_context = ssl._create_unverified_context'のくだりを入れています。これは**teratailのSSLの設定がかしい?**ということなのでしょうか…
31
+ 検証時にChromeで[URL](https://teratail.com/api/v1/users/slash/)を直接開くと問題はなかったのですが、Pythonでそのまま走らせると**teratailのAPIがSSLの認証エラー**ではじかれました。なので、'ssl._create_default_https_context = ssl._create_unverified_context'のくだりを入れています。これはteratailのSSLの設定がかしい?ということなのでしょうか…
32
32
 
33
33
   
34
34
 

2

追補

2017/10/29 22:27

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,9 @@
4
4
 
5
5
 
6
6
 
7
+ 下記のsuyamaさんのアドバイスを受け、[teratailのAPI](http://docs.teratailv1.apiary.io/#reference/(user)/3/0?console=1)を使ってデータを取得出来るところまできましたが、**件数が20件**に絞られています。
8
+
7
- しかし、teratailではクリップの検索ができなかっり、ブラウザで検索しようとするとひたすら「もと見」をクリックするしかないようで
9
+ (「○○件中20件を検索たよ」のよな情報は応答のjson形式の"meta"に収まているようでした
8
10
 
9
11
 
10
12
 
@@ -22,15 +24,17 @@
22
24
 
23
25
  ---
24
26
 
25
- **2017-10-10-29 14:20追記**
27
+ **teratailから提供されているAPIの、Python3での検証コード**
26
28
 
27
- 当面はPythonでやることにしました。
29
+ - **1)**
28
30
 
31
+ 検証時にChromeで[URL](https://teratail.com/api/v1/users/slash/)を直接開くと問題はなかったのですが、Pythonでそのまま走らせるとSSLの認証エラーではじかれました。なので、'ssl._create_default_https_context = ssl._create_unverified_context'のくだりを入れています。これは**teratailのSSLの設定が?かしい?**ということなのでしょうか…
29
32
 
33
+  
30
34
 
31
- 検証時にChromeで[URL](https://teratail.com/api/v1/users/slash/)を直接開くと問題はなかったのですが、Pythonでそのまま走らせるとSSLの認証エラーではじかれました。なので、'ssl._create_default_https_context = ssl._create_unverified_context'のくだりを入れています。これはteratailのSSLがおかしい**(?)**ということなのでしょうか…
35
+ - **2)**
32
36
 
33
-
37
+ **やりすぎるとAPIから`403`**されてしまいます。もし試される方が居ましたらやりすぎには気を付けてください。
34
38
 
35
39
 
36
40
 
@@ -48,6 +52,12 @@
48
52
 
49
53
 
50
54
 
55
+ USER_NAME = "slash"
56
+
57
+
58
+
59
+ # -------------------
60
+
51
61
  # To avoid an error
52
62
 
53
63
  # ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)
@@ -56,28 +66,54 @@
56
66
 
57
67
 
58
68
 
59
- USER_NAME = "slash"
69
+ URL = "https://teratail.com/api/v1/users/" + USER_NAME + "/clips"
60
70
 
71
+
72
+
61
- URL = "https://teratail.com/api/v1/users/" + USER_NAME + "/"
73
+ # -------------------
62
74
 
63
75
 
64
76
 
65
77
  def downloader():
66
78
 
79
+ # Get clip data from teratail
80
+
67
81
  with urllib.request.urlopen(URL) as response:
68
82
 
69
83
  html = response.read()
70
84
 
85
+ text = html.decode('utf-8')
71
86
 
72
87
 
73
- #json_dat = json.loads(text.read)
74
88
 
89
+ # Load clip data as json file
90
+
91
+ json_dat = json.loads(text)
92
+
93
+ # print(json.dumps(json_dat, sort_keys = True, indent = 4)
94
+
95
+ # return json_dat["meta"]
96
+
97
+
98
+
99
+ # Return clip data as dict type
100
+
101
+ return json_dat["questions"]
102
+
103
+
104
+
105
+ def crop_titles(lst_questions):
106
+
107
+ # Crop dictionary from list
108
+
75
- print(html)
109
+ for dct in lst_questions:
110
+
111
+ print( dct["created"] + dct["title"])
112
+
113
+
76
114
 
77
115
  if __name__ == "__main__":
78
116
 
79
- downloader()
117
+ crop_titles(downloader())
80
-
81
-
82
118
 
83
119
  ```

1

現状報告

2017/10/29 22:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,67 @@
17
17
  ***調べたこと**
18
18
 
19
19
  少し前に[teratail の過去遺産をもっと活用したい](https://teratail.com/questions/92505)という記事があり、APIに目を通しましたが「クリップの検索」はないようでした。
20
+
21
+
22
+
23
+ ---
24
+
25
+ **2017-10-10-29 14:20追記**
26
+
27
+ 当面はPythonでやることにしました。
28
+
29
+
30
+
31
+ 検証時にChromeで[URL](https://teratail.com/api/v1/users/slash/)を直接開くと問題はなかったのですが、Pythonでそのまま走らせるとSSLの認証エラーではじかれました。なので、'ssl._create_default_https_context = ssl._create_unverified_context'のくだりを入れています。これはteratailのSSLがおかしい**(?)**ということなのでしょうか…
32
+
33
+
34
+
35
+
36
+
37
+ ```Python
38
+
39
+ # coding: UTF-8
40
+
41
+ import os
42
+
43
+ import urllib.request
44
+
45
+ import json
46
+
47
+ import ssl
48
+
49
+
50
+
51
+ # To avoid an error
52
+
53
+ # ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)
54
+
55
+ ssl._create_default_https_context = ssl._create_unverified_context
56
+
57
+
58
+
59
+ USER_NAME = "slash"
60
+
61
+ URL = "https://teratail.com/api/v1/users/" + USER_NAME + "/"
62
+
63
+
64
+
65
+ def downloader():
66
+
67
+ with urllib.request.urlopen(URL) as response:
68
+
69
+ html = response.read()
70
+
71
+
72
+
73
+ #json_dat = json.loads(text.read)
74
+
75
+ print(html)
76
+
77
+ if __name__ == "__main__":
78
+
79
+ downloader()
80
+
81
+
82
+
83
+ ```