質問するログイン新規登録

質問編集履歴

3

補足情報にページソースの一部を追加しました

2020/05/30 05:48

投稿

takataka_11
takataka_11

スコア2

title CHANGED
File without changes
body CHANGED
@@ -40,5 +40,214 @@
40
40
  ### 試したこと
41
41
  このページソースの単勝オッズと人気の値はおそらく別のサイト?かどこかから値を引っ張て来ているのだろうとは思うのですが、HTMLに関する知識が乏しく、ページソース全体を見渡してみてもどういった構造になっているのかが理解できていない状況です
42
42
  ### 補足情報(FW/ツールのバージョンなど)
43
+ python3.7 windows10
43
44
 
44
- python3.7 windows10
45
+ スクレイピングするサイトのソース一部抜粋
46
+ ```HTML
47
+ <!DOCTYPE html>
48
+ <html>
49
+ <head>
50
+ <meta charset="EUC-JP">
51
+
52
+ _______________________________________________________________
53
+ <tr class="HorseList" id="tr_9">
54
+ <td class="Waku1 Txt_C"><span>1</span></td>
55
+ <td class="Umaban1 Txt_C">1</td>
56
+ <td class="CheckMark Horse_Select">
57
+
58
+ <select class="makeMeFancy_9" name="9" id="mark_9" style="display: none;">
59
+ <option data-html-text="--" value="1_0">--</option><option data-html-text="◎" value="1_1">◎</option><option data-html-text="◯" value="1_2">◯</option><option data-html-text="▲" value="1_3">▲</option><option data-html-text="△" value="1_4">△</option><option data-html-text="☆" value="1_5">☆</option><option data-html-text="&amp;#10003" value="1_98">&amp;#10003</option><option data-html-text="消" value="1_99">消</option>
60
+ </select>
61
+ </td>
62
+ <td class="HorseInfo">
63
+ <div>
64
+ <div>
65
+ <span class="HorseName"><a href="https://db.netkeiba.com/horse/2017104753" target="_blank" title="サトノインプレッサ">サトノインプレッサ<img id="myhorse_2017104753" class="disp_none Favorite" src="https://cdn.netkeiba.com/img.racev3/common/img/icon/icon_horse.png?2019073001" alt="" width="18"></a></span>
66
+ </div>
67
+ </div>
68
+ </td>
69
+ <td class="Barei">牡3</td>
70
+ <td class="Txt_C">57.0</td>
71
+ <td class="Jockey">
72
+ <a href="https://db.netkeiba.com/jockey/01163/" target="_blank" title="坂井">坂井</a>
73
+
74
+
75
+ </td>
76
+ <td class="Trainer"><span class="Label2">栗東</span><a href="https://db.netkeiba.com/trainer/01075/" target="_blank" title="矢作">矢作</a></td>
77
+ <td class="Weight">
78
+
79
+ </td>
80
+ <td class="Txt_R Popular"><span id="odds-1_01" style="font-weight : bold">---.-</span></td>
81
+ <td class="Popular Popular_Ninki Txt_C">
82
+ <span id="ninki-1_01">**</span>
83
+ </td>
84
+ <td class="FavRegist Txt_C">
85
+ <a href="../popup/horse_bookmark.html?ketto_num=2017104753&race_id=202005021211" title="お気に入り馬登録" class="popup_link_04 cboxElement cboxElement_Nologin">
86
+ <img src="https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_01.png?2019073001" class="horsebookmark myhorse1_2017104753" onmouseover="this.src='https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_01.png?2019073001'" onmouseout="this.src='https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_01.png?2019073001'">
87
+ <img id="myhorse1_2017104753" src="https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_02.png?2019073001" class="disp_none horsebookmark myhorse_2017104753" onmouseover="this.src='https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_02.png?2019073001'" onmouseout="this.src='https://cdn.netkeiba.com/img.racev3/common/img/icon/prof_icon_favhorse_comp_02.png?2019073001'">
88
+ </a> </td>
89
+ <td class="FavMemo"><div><a href="../popup/horse_bookmark.html?ketto_num=2017104753&race_id=202005021211" id="Bamei_2017104753" title="" class="popup_link_04 cboxElement"></a></div></td>
90
+ </tr>
91
+ _________________________________________________________________
92
+
93
+ <div style="display:none;" id="free_odds_limit_overlay"></div>
94
+ <script type="text/javascript">
95
+ $(function(){
96
+ //tablesorterの不具合のため、馬番がNULLの場合に馬名のソートの振る舞いを変更する
97
+ $("tbody .HorseInfo").each( function() {
98
+ var rowIndex = $(this).parent().index();
99
+ if ($("tbody [class^=Umaban]").eq(rowIndex).text() == "") {
100
+ $(this).attr("data-sort-value", rowIndex+1);
101
+ }
102
+ });
103
+ // オッズ更新click
104
+ $(".OddsUpdataBtn").on({"click":
105
+ function(){
106
+ $(".OddsUpdataBtn").removeClass("OddsUpdataClick");
107
+ $(".OddsUpdataBtn").addClass("OddsUpdataClick");
108
+ }
109
+ });
110
+ });
111
+ </script>
112
+ <script>
113
+ "use strict"
114
+ $(function(){
115
+ /* Area created in the intern Start */
116
+ const nullCheak = function(date){ return !date[0] ? true : false };
117
+ var firstTime = true;
118
+ const table_sorte = function(t_header_flg){
119
+ $('.Shutuba_Table th').unbind();
120
+ $.tablesorter.destroy( $('.Shutuba_Table'), true, function(table) {});
121
+ $('.Shutuba_Table').tablesorter({
122
+ headers: {
123
+ 0: { sorter: "false" },
124
+ 1: { sorter: t_header_flg[1] },
125
+ 2: { sorter: "false" },
126
+ 3: { sorter: "text" },
127
+ 4: { sorter: "false" },
128
+ 5: { sorter: t_header_flg[5] },
129
+ 6: { sorter: "false" },
130
+ 7: { sorter: "false" },
131
+ 8: { sorter: t_header_flg[8] },
132
+ 9: { sorter: "false" },
133
+ 10: { sorter: t_header_flg[10] },
134
+ 11: { sorter: "false" },
135
+ 12: { sorter: "false" },
136
+ 13: { sorter: "false" },
137
+ },
138
+ //tablesorterの不具合のため、馬番がNULLの場合に馬名のソートの振る舞いを変更する
139
+ textExtraction: function(node) {
140
+ var attr = $(node).attr('data-sort-value');
141
+ if (typeof attr !== 'undefined' && attr !== false) {
142
+ return attr;
143
+ }
144
+ return $(node).text();
145
+ }
146
+ });
147
+ };
148
+ // $(window).load(function(){
149
+ var tableSorteAction = function() {
150
+ const Umaban = $('td.Umaban');
151
+ const weight = $('td.Weight small');
152
+ const t_header_icons = $('.sort_icon');
153
+ // sort flag [0:枠,1:印,2:名,3:オッズ,4:体重]
154
+ var t_sorts_flg = [true,true,true,true,true,true,true,true, true, true, true, false ];
155
+ // sort Icon generation
156
+ const icon = $('<i></i>',{
157
+ 'class' : 'fas fa-sort',
158
+ });
159
+ t_header_icons.append(icon);
160
+ // Empty check in frame order
161
+ if($('td').hasClass('Umaban')){
162
+ t_header_icons[0].remove();
163
+ t_sorts_flg[1] = false;
164
+ }
165
+ // Empty check of horse weight
166
+ if(!weight.length){
167
+ t_header_icons[3].remove();
168
+ // t_sorts_flg[3] = false;
169
+ t_sorts_flg[8] = false;
170
+ }
171
+ table_sorte(t_sorts_flg);
172
+ $('.sort_icon').css("display", "block");
173
+ $('.sort_icon').show();
174
+ };
175
+ /* Area created in the intern End */
176
+ $.oddsUpdate({
177
+ apiUrl:'https://race.netkeiba.com/api/api_get_jra_odds.html',
178
+ raceId:'202005021211',
179
+ isPremium:0,
180
+ displayDiffTime:false,
181
+ PremiumLinkReturnUrl : 'http%3A%2F%2Frace.netkeiba.com%2Frace%2Fshutuba.html%3Frace_id%3D202005021211',
182
+ PremiumLinkReturnRf : 'shutuba',
183
+ isBrackets : false,
184
+ compress:true,
185
+ callbackApiComplete:function(_this,_odds_status,_data){
186
+ // オッズ見出し
187
+ if(_odds_status=='yoso'){
188
+ $('#odds_title').html('予想<br />オッズ');
189
+ } else if(_odds_status=='result'){
190
+ $('#odds_title').html('オッズ');
191
+ } else if(_odds_status=='middle') {
192
+ $('#premium_guide').show();
193
+ $('#odds_title').html('オッズ');
194
+ } else {
195
+ $('#odds_title').html('オッズ');
196
+ }
197
+ // マイオッズ遷移先
198
+ var action = 'https://race.netkeiba.com';
199
+ var action_form_pid = '../odds/myodds';
200
+ if(_odds_status=='yoso'){
201
+ action = '';
202
+ action_form_pid = '../odds/index';
203
+ }
204
+ $('#shutuba_form').attr('action',action_form_pid+".html");
205
+ // オッズ着色
206
+ $('[id^="odds-1_"]').each(function(){
207
+ var data_key = $(this).prop('id').replace('odds-','').split('_');
208
+ var type = data_key[0];
209
+ var no = data_key[1];
210
+ if (_data) {
211
+ if(!$.isEmptyObject(_data['odds'][type])){
212
+ var ary_row = _data['odds'][type];
213
+ var row = ary_row[no];
214
+ if (row) {
215
+ $(this).text(row[0]);
216
+ if(type==1){
217
+ $(this).parent().parent().find(".Popular_Ninki")
218
+ .removeClass("BgYellow")
219
+ .removeClass("BgBlue02")
220
+ .removeClass("BgOrange");
221
+ if (row[2] == 1) {
222
+ $(this).parent().parent().find(".Popular_Ninki").addClass("BgYellow")
223
+ } else if (row[2] ==2) {
224
+ $(this).parent().parent().find(".Popular_Ninki").addClass("BgBlue02")
225
+ } else if (row[2] ==3) {
226
+ $(this).parent().parent().find(".Popular_Ninki").addClass("BgOrange")
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+ $(this).parent().removeClass('Odds_Ninki');
233
+ $(this).removeClass('Odds_Ninki');
234
+ if($(this).text()<10){
235
+ $(this).parent().removeClass('Odds_Ninki');
236
+ $(this).addClass('Odds_Ninki');
237
+ }
238
+ });
239
+ var tmDeleteUpdateOddsClass = setTimeout(function(){
240
+ clearTimeout(tmDeleteUpdateOddsClass);
241
+ $(".Shutuba_Table tr .UpdateOdds").removeClass("UpdateOdds");
242
+ }, 700);
243
+ if (firstTime) {
244
+ firstTime = false;
245
+ tableSorteAction();
246
+ } else {
247
+ $(".Shutuba_Table").trigger('update', [true])
248
+ }
249
+ },
250
+ });
251
+ });
252
+ </script>
253
+ ```

2

ページソースの一部 を編集しました

2020/05/30 05:48

投稿

takataka_11
takataka_11

スコア2

title CHANGED
File without changes
body CHANGED
@@ -4,12 +4,14 @@
4
4
  netkeibaの出馬表ページから単勝オッズと人気をスクレイピングしたい
5
5
  参考ページ:https://race.netkeiba.com/race/shutuba.html?race_id=202005021211
6
6
  ページソースの一部______________________________
7
+ ```HTML
7
8
  <td class="Barei">牡3</td> #馬齢
8
9
  <td class="Txt_C">57.0</td> #斤量
9
10
  <td class="Txt_R Popular"><span id="odds-1_03" style="font-weight : bold">---.-</span></td> #単勝オッズ
10
11
  <td class="Popular Popular_Ninki Txt_C"> 
11
12
  <span id="ninki-1_03">**</span> #人気
12
13
  </td>
14
+ ```
13
15
  ________________________________________
14
16
  python3.7 beautifulsoupを使ってスクレイピングをしています
15
17
  ページソースのタグとclassを頼りにしてtextを取得する形でスクレイピングを試みているのですが、ページソースの単勝オッズ、人気のtextが、ページにでている数値と異なっており正しくスクレイピングできません

1

ソースコードのインデントの修正をしました

2020/05/30 05:37

投稿

takataka_11
takataka_11

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,6 @@
1
+ ```ここに言語を入力
2
+ コード
1
- ### 前提・実現したいこと
3
+ ```### 前提・実現したいこと
2
4
  netkeibaの出馬表ページから単勝オッズと人気をスクレイピングしたい
3
5
  参考ページ:https://race.netkeiba.com/race/shutuba.html?race_id=202005021211
4
6
  ページソースの一部______________________________
@@ -15,6 +17,7 @@
15
17
  ### 発生している問題・エラーメッセージ
16
18
  ページソースのtextにオッズ・人気データが直接書き込まれていないためスクレイピングができない
17
19
  ### 該当のソースコード(一部)#タグとclassを頼りにtextを引き抜いています
20
+ ```python
18
21
  soup = BeautifulSoup(r_text,"html.parser")
19
22
  HorseLists = soup.find_all("tr",class_="HorseList")
20
23
 
@@ -31,6 +34,7 @@
31
34
  Trainer.append(X.strip())
32
35
  X = HorseList.find("td",class_ ="Txt_R Popular").get_text()
33
36
  odds.append(X.strip())
37
+ ```
34
38
  ### 試したこと
35
39
  このページソースの単勝オッズと人気の値はおそらく別のサイト?かどこかから値を引っ張て来ているのだろうとは思うのですが、HTMLに関する知識が乏しく、ページソース全体を見渡してみてもどういった構造になっているのかが理解できていない状況です
36
40
  ### 補足情報(FW/ツールのバージョンなど)