591
No
jsoup 사용하기
package java11;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Craw_3 extends JFrame {
StringBuffer buffer ;
List<AgriculturalDTO> arryList;
JTextField[] texts =new JTextField[10];
JPanel panel;
static JTextArea area;
public String test() {
Document doc;
String result = "";
try {
doc = Jsoup.connect("http://weather.rda.go.kr/weather/observation.jsp").get();
result = doc.title() + "\n\n";
Elements links = doc.select("td");
for (Element link : links) {
result += link.attr("td");
result += link.text();
result = result + "&";
}
parSing(result);
}
catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void parSing(String result){
arryList =new ArrayList<>();
String data1=result.replace("농업기상관측 - 농업기상(농진청)", ""); //제목 제거
String data2 =data1.replace("\n\n", ""); //엔터 2개 제거
StringTokenizer st =new StringTokenizer(data2, "&");
int countTokens=st.countTokens();
int numIndex=0;
AgriculturalDTO dto=null;
for(int i=0; i<countTokens; i++){
String token =st.nextToken();
if(token==null){
token="";
}
switch (i%10) {
case 0:
dto =new AgriculturalDTO();
dto.setIdx(++numIndex);
dto.setBranchName(token); //지점명
break;
case 1:
dto.setTemperatures(token);//기온
break;
case 2:
dto.setHumidity(token); //습도
break;
case 3:
dto.setWindDirection(token);//풍향
break;
case 4:
dto.setAverageWindSpeed(token);//평균풍속
break;
case 5:
dto.setMomentMaximumWindSpeed(token); //순간 최대풍속
break;
case 6:
dto.setPrecipitation(token);//강수량
break;
case 7:
dto.setDaylightHours(token);//일조 시간
break;
case 8:
dto.setSoilMoistureCurrent(token);//토양수분 현재 상태
break;
case 9:
dto.setSoilMoistureLastYear(token);//토양수분 작년
arryList.add(dto);
break;
}
}
}
public static void main(String[] args) {
/*
Craw_3 craw =new Craw_3();
craw.test();
//System.out.println(craw.test().toString());
if(craw.arryList!=null){
for(AgriculturalDTO dto : craw.arryList ){
System.out.println(dto.toString() );
}
}*/
Craw_3 craw =new Craw_3();
craw.test();
if(craw.arryList!=null){
for(AgriculturalDTO dto : craw.arryList ){
System.out.println(dto.toString() );
area.append(dto.getIdx() + " ---- " + dto.getBranchName()+ " ---- " + dto.getTemperatures()
+"℃ ---- " +dto.getHumidity() +" ---- " + dto.getDaylightHours() +"\n");
}
}
}
public Craw_3() {
panel =new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("번호 ----- 지점명 ----- 기온( ℃ ) --- 습도 ----- 날짜 "));
area=new JTextArea(3, 30); //3행 30열
// 폰트 사이즈 조절
area.setFont(area.getFont().deriveFont(15f));
//스크롤 기능 추가
JScrollPane scrollPane =new JScrollPane();
panel.add(scrollPane);//패널에 스크롤페인 추가
// 스크롤페인의 뷰포트에 TextArea 를 설정
scrollPane.setViewportView(area);
add(panel, BorderLayout.NORTH);// panel 을 North 추가
add(scrollPane, "Center");// 스크롤페인을 Center 추가
setSize(700, 500);// 프레임의 크기 설정(가로, 세로)
setVisible(true);// 프레임을 표시
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 프레임의 닫기 동작
}
}
AgriculturalDTO
package java11;
public class AgriculturalDTO {
private int idx; //인덱스
private String branchName; //지점명
private String temperatures; //기온
private String humidity; //습도
private String windDirection; //풍향
private String averageWindSpeed; //평균풍속
private String momentMaximumWindSpeed; //순간 최대풍속
private String precipitation; //강수량
private String daylightHours; //일조 시간
private String soilMoistureCurrent ; //토양수분 현재 상태
private String soilMoistureLastYear; //토양수분 작년
public int getIdx() {
return idx;
}
public void setIdx(int idx) {
this.idx = idx;
}
public String getBranchName() {
return branchName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
public String getTemperatures() {
return temperatures;
}
public void setTemperatures(String temperatures) {
this.temperatures = temperatures;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getWindDirection() {
return windDirection;
}
public void setWindDirection(String windDirection) {
this.windDirection = windDirection;
}
public String getAverageWindSpeed() {
return averageWindSpeed;
}
public void setAverageWindSpeed(String averageWindSpeed) {
this.averageWindSpeed = averageWindSpeed;
}
public String getMomentMaximumWindSpeed() {
return momentMaximumWindSpeed;
}
public void setMomentMaximumWindSpeed(String momentMaximumWindSpeed) {
this.momentMaximumWindSpeed = momentMaximumWindSpeed;
}
public String getPrecipitation() {
return precipitation;
}
public void setPrecipitation(String precipitation) {
this.precipitation = precipitation;
}
public String getDaylightHours() {
return daylightHours;
}
public void setDaylightHours(String daylightHours) {
this.daylightHours = daylightHours;
}
public String getSoilMoistureCurrent() {
return soilMoistureCurrent;
}
public void setSoilMoistureCurrent(String soilMoistureCurrent) {
this.soilMoistureCurrent = soilMoistureCurrent;
}
public String getSoilMoistureLastYear() {
return soilMoistureLastYear;
}
public void setSoilMoistureLastYear(String soilMoistureLastYear) {
this.soilMoistureLastYear = soilMoistureLastYear;
}
@Override
public String toString() {
return "AgriculturalDTO [idx=" + idx + ", branchName=" + branchName + ", temperatures=" + temperatures
+ ", humidity=" + humidity + ", windDirection=" + windDirection + ", averageWindSpeed="
+ averageWindSpeed + ", momentMaximumWindSpeed=" + momentMaximumWindSpeed + ", precipitation="
+ precipitation + ", daylightHours=" + daylightHours + ", soilMoistureCurrent=" + soilMoistureCurrent
+ ", soilMoistureLastYear=" + soilMoistureLastYear + "]";
}
}
댓글 ( 4)
댓글 남기기