0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Seleniumでデータを集めたい

Posted at

背景

javaのSeleniumを使って競馬のデータをスクレイピングを行なっており、今は各馬の戦績を収集しています。
収集元URL

現状

URLを見てみてください。
各試合ごとのデータを取得したいです。
イメージはこんな感じ

String[] eachRecode1 = {"2021/06/06","3東京2","曇","11".....};
String[] eachRecode2 = {"2021/02/07","1東京4",.....};
String[] eachRecode3 = {"2021/01/10",.....};
List<String[]> recodes = new ArrayList<String[]>();
recodes.add(eachRecode1);
recodes.add(eachRecode2);
recodes.add(eachRecode3);
//本当はfor文を使ってうまくやります。

具体的には
//table[@summary="カラテの競走戦績"]/tbodyにあるtrをeachRecodeに対応させて
tdをeachRecodesの要素として取得します。

これを行うためのプログラムの一部が下記です。

for(int i = 0; i < recodeListsElement.size(); i++) {
	List<WebElement> we = dri.findElements(By.xpath("//table[@summary=\""+hoseName+"の競走戦績\"]/tbody/tr["+i+"]"));
	for(WebElement e: we) {
		System.out.println(e.getText());
		recode[i] = e.getText();
	}
	System.out.println("***********************:");
	recodeList.add(recode);
}

問題点

上記のプログラムを実行すると下記のように表示されます。
自分はtdの要素がSystem.out.println(e.getText());で表示されると思って記述しました。
ですが、どう工夫してもできません。

***********************:
2021/06/06 3東京2 曇 11 安田記念(G1)
14 3 4 77.5 10 13 菅原明良 58 芝1600 良
**
1:33.0 1.3
**
12-12 34.9-33.9 34.1 526(-4)
ダノンキングリー  
***********************:
2021/02/07 1東京4 晴 11 東京新聞杯(G3)
16 5 10 11.6 5 1 菅原明良 56 芝1600 良
**
1:32.4 0.0
**
5-4 34.9-34.3 34.0 530(+2)
  (カテドラル) 3,957.4
***********************:

理想系↓

2021/06/06
***********************:
3東京2
***********************:
.
.
.

取り組み

Xpathの指定に問題があるのかと思い調べました。following-sibling::td[1]などを使い色々工夫ひてみたのですがうまくできません。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?