java + beautifulsoup

Java2019. 2. 9. 08:42

package t01;


import java.io.IOException;

import java.text.Format;

import java.time.format.FormatStyle;


import org.json.JSONArray;

import org.json.JSONObject;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;


public class crawl02 {

// ==========================

String url = null;

Document doc = null;

Elements items = null;

JSONArray  jsonArray  = null;

// ==========================

public crawl02(String paramHtml) {

// TODO Auto-generated constructor stub

this.url = paramHtml;

}

public JSONArray urlRequests() {

try {

this.doc = Jsoup.connect(this.url).get();

this.jsonArray = new JSONArray();

for (int p = 1; p <= 2; p++) {

this.items = this.doc.select("ul.ah_l")

.get(p)

.select("li.ah_item");

StringBuilder sb = new StringBuilder();

for (Element t : this.items) {

//System.out.print(t.select("span.ah_k"));

t = t.select("a").get(0);

JSONObject jsonObject = new JSONObject();

jsonObject.put("rnk", t.select("a span.ah_r").text());

jsonObject.put("txt", t.select("span.ah_k").text());

jsonArray.put(jsonObject);

}

// 테스트 코드 

// sb.append("순위 : ");

// sb.append(t.select("a span.ah_r").text());

// sb.append(" ");

// sb.append("내용 : ");

// sb.append(t.select("span.ah_k").text());

// sb.append("\n");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(jsonArray);

return jsonArray;

}

}



'Java' 카테고리의 다른 글

stu  (0) 2019.03.11
java + mysql 연동  (0) 2019.02.07
java yaml 파일 읽기  (0) 2019.01.20
java_yaml  (0) 2018.12.16