Newer
Older
ScrapeRecipe / WEB-INF / src / servlet / Scrape.java
matsumoto_k on 8 May 2017 2 KB first commit
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import net.arnx.jsonic.JSON;

public class Scrape extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("application/json;charset=UTF-8");

		String url = request.getParameter("recipeUrl");

		/* HTML 出力用 PrintWriter */
		PrintWriter out = response.getWriter();

		org.jsoup.nodes.Document document = Jsoup.connect(url).get();
		Elements anyone = document.select("div.materialTit h3 span span");
		Elements tmp = document.select("div.materialBox ul li");

		Elements material = new Elements();
		Elements amount = new Elements();

		JsonData jsonData = new JsonData();
		jsonData.anyone = anyone.text();
		MaterialJsonData materialJsonData;

		for (int i = 0; i < tmp.size(); i++) {
			materialJsonData = new MaterialJsonData();
			if (tmp.get(i).select(".amount").isEmpty() != true) {
				material.addAll(tmp.get(i).select("a"));
				amount.addAll(tmp.get(i).select("p"));
				materialJsonData.material = tmp.get(i).select("a").text();
				materialJsonData.amount = tmp.get(i).select("p").text();
			} else {
				material.addAll(tmp.get(i).select("a"));
				amount.addAll(tmp.get(i).select("a"));
				materialJsonData.material = tmp.get(i).select("a").text();
				materialJsonData.amount = tmp.get(i).select("a").text();
			}
			jsonData.material.add(materialJsonData);
		}

		Elements howto = document.select("p.stepMemo");
		for (int i = 0; i < howto.size(); i++) {
			HowToJsonData howToJsonData = new HowToJsonData();
			howToJsonData.step = howto.get(i).text();
			jsonData.howto.add(howToJsonData);
		}

		String responseJson = JSON.encode(jsonData, true);

		out.print(responseJson);

		out.close();
	}

	public Scrape() {
		// TODO Auto-generated constructor stub
	}
}