質問編集履歴

3

説明の追加

2023/05/13 08:07

投稿

cheriraoka
cheriraoka

スコア42

test CHANGED
File without changes
test CHANGED
@@ -9,12 +9,75 @@
9
9
  ### 試したコード
10
10
 
11
11
  ```ここに言語名を入力
12
+ url = 'https://race.netkeiba.com/race/newspaper.html?race_id=202305020504&rf=shutuba_submenu'
13
+ browser.implicitly_wait(3)
14
+ browser.get(url)
15
+
16
+
17
+ dataindex_list = ["//*[@data-index='{}']".format(i) \
18
+ for i in range(len(browser.find_elements(By.CSS_SELECTOR, "dl.HorseList")))]#各レースにおける頭数
19
+ list_1 = []
20
+ list_2 = []
21
+ list_3 = []
22
+ list_4 = []
23
+
24
+ for dataindex in dataindex_list:
25
+ #horse_idを抽出
12
- elem_urls = []
26
+ elem_urls = []
27
+ elem = browser.find_element(By.XPATH, dataindex)
13
- elems = browser.find_elements(By.CLASS_NAME, 'Horse06 fc')
28
+ elems = elem.find_elements(By.CLASS_NAME, 'Horse02')
14
- for elem in elems:
29
+ for elem in elems:
30
+ elem_urls.append(elem.find_element(By.TAG_NAME, 'a').get_attribute('href'))
31
+ horse_id = []
32
+ for elem_url in elem_urls:
33
+ elem_url = re.findall(r'\d+', elem_url)
34
+ horse_id.append(elem_url[0])
35
+ elem = browser.find_element(By.XPATH, dataindex)
36
+ horse_id = horse_id * len(elem.find_elements(By.CLASS_NAME, 'Data01')) #前走の数だけhorse_idを取得
37
+ list_1.extend(horse_id)
38
+
39
+ #馬が走ったrace_idを抽出
40
+ elem = browser.find_element(By.XPATH, dataindex)
41
+ elems = elem.find_elements(By.CSS_SELECTOR, "span.RaceName")
42
+ elem_urls = []
43
+ for elem in elems:
44
+ elem_urls.append(elem.find_element(By.TAG_NAME, 'a').get_attribute('href'))
45
+
46
+ race_id_list = []
47
+ for elem_url in elem_urls:
48
+ elem_url = re.findall(r'\d+', elem_url)
49
+ race_id_list.append(elem_url[0])
50
+ list_2.extend(race_id_list)
51
+
52
+ #前半3Fタイムを抽出
53
+ elem = browser.find_element(By.XPATH, dataindex)
54
+ elems = elem.find_elements(By.CSS_SELECTOR, "span.Data19")
55
+ row = []
56
+ for elem in elems:
57
+ text = elem.text
58
+ text = re.findall(r'\d+\.\d+', text)
59
+ text = "".join(text)
60
+ row.append(text)
61
+ list_3.extend(row)
62
+
63
+ #脚質をスクリーニング
64
+ elem = browser.find_element(By.XPATH, dataindex)
15
- elem_urls.append(elem.find_element(By.CSS_SELECTOR, 'div.Type Type04'))
65
+ elems = elem.find_elements(By.CSS_SELECTOR, '.Horse06.fc')
66
+ div_class = []
67
+ for elem in elems:
68
+ div_class = elem.find_element(By.CSS_SELECTOR, 'div.Type').get_attribute('class')
69
+ div_class = div_class[-1] #Type Type01だったら1のみ抽出
70
+ list_4.extend(div_class)
71
+ list_4 = [list_4[0]] * len(list_1)
72
+
73
+ df = pd.DataFrame()
74
+ df['horse_id'] = list_1
75
+ df['race_id'] = list_2
76
+ df['前半'] = list_3
77
+ df['脚質'] = list_4
16
78
  ```
17
- 他にもget_attributeを使用し、何度も試行錯誤しましたがうくできませんでした
79
+ 上のコードで試しました。しかし、horse_id毎に1~4の脚質にたいのですが脚質がすべて同じになってします(画像参照)
80
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-05-13/ab4002fe-a3cb-4f6e-a58d-635b2af0c14c.png)
18
81
 
19
82
  ### htmlについて
20
83
  <dl class="HorseList" data-index="0" id="past_tr_8">

2

誤字

2023/05/13 03:19

投稿

cheriraoka
cheriraoka

スコア42

test CHANGED
File without changes
test CHANGED
@@ -25,5 +25,5 @@
25
25
       ::after
26
26
      </div>
27
27
 
28
- htmlについては上のような感じですhttps://race.netkeiba.com/race/newspaper.html?race_id=202305020412の左側にある馬名欄にある◀印のものを抽出したいです。
28
+ htmlについては上のような感じですhttps://race.netkeiba.com/race/newspaper.html?race_id=202305020412 の左側にある馬名欄にある◀印のものを抽出したいです。
29
29
  dl classのdata-indexは馬名毎に番号が変わり、div classのTypeは◀の位置によってType01~04が付与させています。

1

説明の追加

2023/05/13 03:17

投稿

cheriraoka
cheriraoka

スコア42

test CHANGED
File without changes
test CHANGED
@@ -15,3 +15,15 @@
15
15
  elem_urls.append(elem.find_element(By.CSS_SELECTOR, 'div.Type Type04'))
16
16
  ```
17
17
  他にもget_attributeを使用し、何度も試行錯誤しましたがうまくできませんでした。
18
+
19
+ ### htmlについて
20
+ <dl class="HorseList" data-index="0" id="past_tr_8">
21
+  <dt class="Horse_Info orderfix" style="width: 179.52px;">
22
+   <dl class="fc">
23
+    <dt class="Horse06 fc">
24
+     <div class="Type Type01">
25
+      ::after
26
+     </div>
27
+
28
+ htmlについては上のような感じですhttps://race.netkeiba.com/race/newspaper.html?race_id=202305020412の左側にある馬名欄にある◀印のものを抽出したいです。
29
+ dl classのdata-indexは馬名毎に番号が変わり、div classのTypeは◀の位置によってType01~04が付与させています。