質問編集履歴
2
python3.5のスクリプトに改変しました、再度御教示お願いいたします。
test
CHANGED
File without changes
|
test
CHANGED
@@ -149,3 +149,225 @@
|
|
149
149
|
|
150
150
|
|
151
151
|
二行めの16 を取る方法を 御教示 よろしくお願いいたします。
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
umyu様、丁寧な御教示ありがとうございます。
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
うまく動きました。ただ、
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
ご助言にもございましたが、小生、python3.5に移行中でございます。
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
そのため、今回のスクリプトをpython3.5に改変しましたところ、
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
以下のようなスクリプトになりました。
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```ここに言語を入力
|
186
|
+
|
187
|
+
# -*- coding:utf-8 -*-
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
import urllib.request
|
192
|
+
|
193
|
+
import codecs
|
194
|
+
|
195
|
+
import time
|
196
|
+
|
197
|
+
from bs4 import BeautifulSoup
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
f1 = codecs.open('tokyo1.csv', 'w', 'utf-8')
|
202
|
+
|
203
|
+
f1.write('other_race_name'+u"\n")
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
url_1='http://race.netkeiba.com/?pid=race_old&id=c201805010801'
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
soup_1 = BeautifulSoup(urllib.request.urlopen(url_1), "html.parser")
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
#race1
|
216
|
+
|
217
|
+
tr_arr_1 = soup_1.select("table.race_table_old > tr ")
|
218
|
+
|
219
|
+
for tr_1 in tr_arr_1:
|
220
|
+
|
221
|
+
#time.sleep(0.25)
|
222
|
+
|
223
|
+
tds_1 = tr_1.findAll("td")
|
224
|
+
|
225
|
+
if len( tds_1 ) > 1:
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).find('p')
|
230
|
+
|
231
|
+
other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
|
232
|
+
|
233
|
+
cols = [other_race_name_1.strip()]
|
234
|
+
|
235
|
+
f1.write(",".join(cols) + "\n")
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
print (other_race_name_1.strip())
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
f1.close()
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
上記のスクリプトは一行目まではうまく動きます。
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
しかしながら、今回の目的の二行目までを取得しようとして、
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
御教示いただいたように、下記の通りに改変しましたが、うまく動きませんでした。
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
```ここに言語を入力
|
270
|
+
|
271
|
+
# -*- coding:utf-8 -*-
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
import urllib.request
|
276
|
+
|
277
|
+
import codecs
|
278
|
+
|
279
|
+
import time
|
280
|
+
|
281
|
+
from bs4 import BeautifulSoup
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
f1 = codecs.open('tokyo1.csv', 'w', 'utf-8')
|
286
|
+
|
287
|
+
f1.write('other_race_name'+u"\n")
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
url_1='http://race.netkeiba.com/?pid=race_old&id=c201805010801'
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
soup_1 = BeautifulSoup(urllib.request.urlopen(url_1), "html.parser")
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
#race1
|
300
|
+
|
301
|
+
tr_arr_1 = soup_1.select("table.race_table_old > tr ")
|
302
|
+
|
303
|
+
for tr_1 in tr_arr_1:
|
304
|
+
|
305
|
+
#time.sleep(0.25)
|
306
|
+
|
307
|
+
tds_1 = tr_1.findAll("td")
|
308
|
+
|
309
|
+
if len( tds_1 ) > 1:
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).find_all('p')
|
314
|
+
|
315
|
+
other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
|
316
|
+
|
317
|
+
cols = [other_race_name_1.strip()]
|
318
|
+
|
319
|
+
f1.write(",".join(cols) + "\n")
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
print (other_race_name_1.strip())
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
f1.close()
|
328
|
+
|
329
|
+
```
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
下記のエラーが出てきました。
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
```ここに言語を入力
|
352
|
+
|
353
|
+
Traceback (most recent call last):
|
354
|
+
|
355
|
+
File "C:\Users\satoru\satoru_system\race_data_scan\tokyo\1_tokyo_race_data_scan.py", line 23, in <module>
|
356
|
+
|
357
|
+
other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
|
358
|
+
|
359
|
+
File "C:\Users\satoru\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\element.py", line 1807, in __getattr__
|
360
|
+
|
361
|
+
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
|
362
|
+
|
363
|
+
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?```
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
python3.5におけるfindの使い方について、再度ご助言頂けますと助かります。
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
よろしくお願いいたします。
|
1
試したことと、うまく動かない結果について追記させていただきました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -56,7 +56,7 @@
|
|
56
56
|
|
57
57
|
|
58
58
|
|
59
|
-
上記の
|
59
|
+
上記のスクリプトでは、一行目の 1回 東京3日目 3歳 は 取れるのですが、
|
60
60
|
|
61
61
|
|
62
62
|
|
@@ -64,7 +64,87 @@
|
|
64
64
|
|
65
65
|
|
66
66
|
|
67
|
+
|
68
|
+
|
69
|
+
試したこと
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
二行目を取る方法として、当該ソースコードを 見てみましたところ以下のような構造でした。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```ここに言語を入力
|
78
|
+
|
79
|
+
1R
|
80
|
+
|
81
|
+
</dt>
|
82
|
+
|
83
|
+
<dd>
|
84
|
+
|
85
|
+
<h1>3歳未勝利</h1>
|
86
|
+
|
87
|
+
<p><span>ダ1300m / 天気:晴 / 馬場:不良 / 発走:11:00</span></p>
|
88
|
+
|
89
|
+
</dd>
|
90
|
+
|
91
|
+
</dl>
|
92
|
+
|
93
|
+
<div class="race_otherdata">
|
94
|
+
|
95
|
+
<p>1回東京3日目 3歳 </p>
|
96
|
+
|
97
|
+
<p>牝[指定] 16頭</p>
|
98
|
+
|
99
|
+
<p>本賞金:500、200、130、75、50万円</p>
|
100
|
+
|
101
|
+
</div>
|
102
|
+
|
103
|
+
<ul class="btn_link_list fc">
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
上記の 1回 東京3日目 3歳はとれるので、<p> </p>で囲まれている全てを取ろうと考えて、上記のスクリプトのfindの箇所を
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```ここに言語を入力
|
116
|
+
|
117
|
+
other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).findall('p')
|
118
|
+
|
119
|
+
other_race_name_1 = "".join([x for x in other_race_name_tag_1.text if not x == u'\xa0' and not x == u'\n'])
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
findall に改変して試しましたが、
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
```ここに言語を入力
|
132
|
+
|
133
|
+
other_race_name_tag_1 = soup_1.find('div',{'class':'race_otherdata'}).findall('p')
|
134
|
+
|
135
|
+
TypeError: 'NoneType' object is not callable
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
67
|
-
|
145
|
+
と、エラーが出てきました。Typeerrorについて調べましたが、うまく動きませんでした。
|
146
|
+
|
147
|
+
|
68
148
|
|
69
149
|
|
70
150
|
|