回答編集履歴
5
コード追加
test
CHANGED
@@ -217,3 +217,33 @@
|
|
217
217
|
return ""
|
218
218
|
|
219
219
|
```
|
220
|
+
|
221
|
+
リストの場合は、そのそれぞれがtextを持っているようです。
|
222
|
+
|
223
|
+
以下に変更して、何が表示されるか見てみてください。
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
```python
|
228
|
+
|
229
|
+
def to_text(elem):
|
230
|
+
|
231
|
+
if type(elem) == list:
|
232
|
+
|
233
|
+
try:
|
234
|
+
|
235
|
+
return ' : '.join([x.text for x in elem])
|
236
|
+
|
237
|
+
except AttributeError:
|
238
|
+
|
239
|
+
return 'need more check'
|
240
|
+
|
241
|
+
try:
|
242
|
+
|
243
|
+
return elem.text
|
244
|
+
|
245
|
+
except AttributeError:
|
246
|
+
|
247
|
+
return ""
|
248
|
+
|
249
|
+
```
|
4
修正
test
CHANGED
@@ -206,7 +206,7 @@
|
|
206
206
|
|
207
207
|
print(f'dir is:\n', dir(e))
|
208
208
|
|
209
|
-
exit()
|
209
|
+
exit()
|
210
210
|
|
211
211
|
try:
|
212
212
|
|
3
デバッグ用コード
test
CHANGED
@@ -186,4 +186,34 @@
|
|
186
186
|
|
187
187
|
```
|
188
188
|
|
189
|
+
|
190
|
+
|
191
|
+
情報を取るためにto_textを以下に変更して、出力のその部分を教えてください。
|
192
|
+
|
193
|
+
|
194
|
+
|
189
|
-
|
195
|
+
```python
|
196
|
+
|
197
|
+
def to_text(elem):
|
198
|
+
|
199
|
+
if type(elem) == list:
|
200
|
+
|
201
|
+
print(f'from to_text:')
|
202
|
+
|
203
|
+
for i,e in enumerate(elem):
|
204
|
+
|
205
|
+
print(f'elem[{i}] = {i}')
|
206
|
+
|
207
|
+
print(f'dir is:\n', dir(e))
|
208
|
+
|
209
|
+
exit()
|
210
|
+
|
211
|
+
try:
|
212
|
+
|
213
|
+
return elem.text
|
214
|
+
|
215
|
+
except AttributeError:
|
216
|
+
|
217
|
+
return ""
|
218
|
+
|
219
|
+
```
|
2
バグ修正
test
CHANGED
@@ -158,11 +158,11 @@
|
|
158
158
|
|
159
159
|
print(pitch_position)
|
160
160
|
|
161
|
-
text_elem_1B = to_text(elem_1B
|
161
|
+
text_elem_1B = to_text(elem_1B)
|
162
162
|
|
163
|
-
text_elem_2B = to_text(elem_2B
|
163
|
+
text_elem_2B = to_text(elem_2B)
|
164
164
|
|
165
|
-
text_elem_3B = to_text(elem_3B
|
165
|
+
text_elem_3B = to_text(elem_3B)
|
166
166
|
|
167
167
|
print(text_elem_1B)
|
168
168
|
|
1
コード追加
test
CHANGED
@@ -39,3 +39,151 @@
|
|
39
39
|
|
40
40
|
|
41
41
|
とかに変えてみてください。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
上記には誤りがありました。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
.textを取る部分を関数にしたので、全コードを載せます。
|
50
|
+
|
51
|
+
また、書込み先のファイルを閉じていなかったので閉じておきました。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```python
|
56
|
+
|
57
|
+
from selenium import webdriver
|
58
|
+
|
59
|
+
import chromedriver_binary
|
60
|
+
|
61
|
+
import time
|
62
|
+
|
63
|
+
import csv
|
64
|
+
|
65
|
+
import datetime
|
66
|
+
|
67
|
+
from selenium.common.exceptions import NoSuchElementException
|
68
|
+
|
69
|
+
import re
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def to_text(elem):
|
74
|
+
|
75
|
+
try:
|
76
|
+
|
77
|
+
return elem.text
|
78
|
+
|
79
|
+
except AttributeError:
|
80
|
+
|
81
|
+
return ""
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
driver = webdriver.Chrome()
|
86
|
+
|
87
|
+
driver.get('https://baseball.yahoo.co.jp/npb/game/2020082103/score?index=0110100')
|
88
|
+
|
89
|
+
csv_date = datetime.datetime.today().strftime("%Y%m%d")
|
90
|
+
|
91
|
+
csv_file_name = "carp_data_" + csv_date + ".csv"
|
92
|
+
|
93
|
+
f = open(csv_file_name, "w", encoding="CP932", errors="ignore")
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
writer = csv.writer(f, lineterminator="\n")
|
98
|
+
|
99
|
+
csv_header = ["球数", "投手", "投", "打者", "打席", "球種", "球速", "結果", "コース", "一塁", "二塁", "三塁"]
|
100
|
+
|
101
|
+
writer.writerow(csv_header)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
i = 0
|
106
|
+
|
107
|
+
item = 1
|
108
|
+
|
109
|
+
while True :
|
110
|
+
|
111
|
+
i = i + 1
|
112
|
+
|
113
|
+
time.sleep(5)
|
114
|
+
|
115
|
+
try:
|
116
|
+
|
117
|
+
elem_pitcher = driver.find_element_by_xpath('//*[@id="gm_rslt"]/tbody/tr/td[1]/a')
|
118
|
+
|
119
|
+
elem_pitch = driver.find_element_by_xpath('//*[@id="gm_rslt"]/tbody/tr/td[2]')
|
120
|
+
|
121
|
+
elem_batter = driver.find_element_by_xpath('//*[@id="gm_rslt"]/tbody/tr/td[3]/a')
|
122
|
+
|
123
|
+
elem_bat = driver.find_element_by_xpath('//*[@id="gm_rslt"]/tbody/tr/td[4]')
|
124
|
+
|
125
|
+
elem_1B = driver.find_elements_by_xpath('//*[@id="base1"]/span')
|
126
|
+
|
127
|
+
elem_2B = driver.find_elements_by_xpath('//*[@id="base2"]/span')
|
128
|
+
|
129
|
+
elem_3B = driver.find_elements_by_xpath('//*[@id="base3"]/span')
|
130
|
+
|
131
|
+
except NoSuchElementException:
|
132
|
+
|
133
|
+
pass
|
134
|
+
|
135
|
+
except AttributeError:
|
136
|
+
|
137
|
+
pass
|
138
|
+
|
139
|
+
elems_tb = driver.find_elements_by_xpath('//*[@id="pitchesDetail"]/section[2]/table[3]/tbody/tr/td[3]')
|
140
|
+
|
141
|
+
elems_tc = driver.find_elements_by_xpath('//*[@id="pitchesDetail"]/section[2]/table[3]/tbody/tr/td[4]')
|
142
|
+
|
143
|
+
elems_te = driver.find_elements_by_xpath('//*[@id="pitchesDetail"]/section[2]/table[3]/tbody/tr/td[5]')
|
144
|
+
|
145
|
+
elems_td = driver.find_elements_by_xpath('//*[@id="pitchesDetail"]/section[2]/table[1]/tbody/tr/td/div/span')
|
146
|
+
|
147
|
+
for elem_tb, elem_tc, elem_te, elem_td in zip(elems_tb, elems_tc, elems_te, elems_td):
|
148
|
+
|
149
|
+
print(elem_pitcher.text)
|
150
|
+
|
151
|
+
print(elem_pitch.text)
|
152
|
+
|
153
|
+
print(elem_batter.text)
|
154
|
+
|
155
|
+
print(elem_bat.text)
|
156
|
+
|
157
|
+
pitch_position = elem_td.get_attribute('style')
|
158
|
+
|
159
|
+
print(pitch_position)
|
160
|
+
|
161
|
+
text_elem_1B = to_text(elem_1B.text)
|
162
|
+
|
163
|
+
text_elem_2B = to_text(elem_2B.text)
|
164
|
+
|
165
|
+
text_elem_3B = to_text(elem_3B.text)
|
166
|
+
|
167
|
+
print(text_elem_1B)
|
168
|
+
|
169
|
+
print(text_elem_2B)
|
170
|
+
|
171
|
+
print(text_elem_3B)
|
172
|
+
|
173
|
+
csvlist = [str(item), elem_pitcher.text, elem_pitch.text, elem_batter.text, elem_bat.text, elem_tb.text, elem_tc.text, elem_te.text, text_elem_1B, text_elem_2B, text_elem_3B]
|
174
|
+
|
175
|
+
writer.writerow(csvlist)
|
176
|
+
|
177
|
+
item = item + 1
|
178
|
+
|
179
|
+
next_link = driver.find_element_by_id('btn_next')
|
180
|
+
|
181
|
+
driver.get(next_link.get_attribute('href'))
|
182
|
+
|
183
|
+
driver.close()
|
184
|
+
|
185
|
+
f.close()
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
これで動きますか?
|