11
7

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 5 years have passed since last update.

Jsoupを使って株価をスクレイピング

Last updated at Posted at 2015-08-28

Jsoupを使って株価をスクレイピングできるようになったのでメモ。
最終的には取得した値をまとめてメールで送れるようにする予定です。

Parser.java
package parser;
import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Parser {

	// コンストラクタでURLを指定するので、private
	@SuppressWarnings("unused")
	private Parser() {
	};

	protected Document document = null;
	public Parser(String url) throws IOException {
		this.document = Jsoup.connect(url).get();
	}

	public ArrayList<String> getElementsByClass(String className) {
		ArrayList<String> list = new ArrayList<>();
		Elements elements = document.getElementsByClass(className);
		for (Element element : elements) {
			list.add(element.text());
		}
		return list;
	}
}
Main.java
package main;
import java.io.IOException;

import parser.Parser;

public class Main {
	public static void main(String[] args) throws IOException {
		// 今回はトヨタの株価を取得してみる。
		String kabuka = new Parser("http://stocks.finance.yahoo.co.jp/stocks/detail/?code=7203.T").getElementsByClass("stoksPrice").get(1);
		System.out.println(kabuka);
	}
}
11
7
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
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?