UI
CUI - Character(Text)
: 명령 프롬프트(dir ....) = Terminal
GUI - Graphic(2D->3D)
: 2D, 3D graphic 기반 -> 각 언어마다 다름
ex) java
: Oracle (AWT / Swing / Java Fx)
Eclipse (SWT)
: 변수 / 상수
로컬 - file
: text(csv)
: 특수파일(xlsx / image / sound / 동영상)
원격
데이터베이스(데이터크롤링 or 스크래핑) <- 네트워크 필요
: text(csv) / xml / json
OpenAPI - 공식
: 공공기관[data.go.kr])
: 구글 developer.google.com
: 네이버/다음
: 영화 www.kobis.or.kr/
html 분석 - 비공식
구분
라우터 내부
인트라넷 - 사내망
: 192.168.xxx.xxx
라우터 외부
인터넷- 외부망
: 그 외의 아이피
IP - 전 세계의 컴퓨터(네트워크 카드)
: IPv4 -> IPv6
Mac
고유번호
네트워크 카드 + 프로그램
외부에서 프로그램에 접근
: 프로토콜(전송규약)
: ip
: port
Well Known Port
프로토콜 <-> port(기본값은 생략가능)
http 80 / 8080
https 443
mail 25
mariaDB 3306
ipconfig | ipconfig /all
ping
tracert - gsuite.tools/traceroute
netstat -an
클라이언트(요청) <- 네트워크 -> 서버(응답) => C/S
: 브라우저와 웹서버(톰캣)
: mysql 명령어(Heidi)와 mariaDB 서비스
P2P(Peer to Peer) : 클라이언트와 서버역할을 동시에 함
-> 블럭체인
접속 -> url
읽기 -> I/O
public static void main(String[] args) {
// TODO Auto-generated method stub
// network - java.net
// IP 정보 획득하기 <- 도메인
try {
InetAddress inetAddress1 = InetAddress.getByName("www.daum.net");
System.out.println(inetAddress1.getHostAddress());
System.out.println(inetAddress1.getHostName());
// 네이버의 경우 ip가 여러개 -> 배열 형식으로 출력
InetAddress[] inetAddresses = InetAddress.getAllByName("www.naver.com");
for(InetAddress inetAddress : inetAddresses) {
System.out.println(inetAddress.getHostAddress());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// URL 정보
try {
// 각각 쪼개서 출력
String strUrl = "https://search.naver.com:443/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=covid";
URL url = new URL(strUrl);
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream is = null;
try {
// URL
URL url = new URL("https://m.daum.net");
is = url.openStream();
// I/O
int data = 0;
while((data = is.read()) != -1) {
System.out.println((char)data);
}
System.out.println();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} finally {
if(is != null) try {is.close();} catch(IOException e) {}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null; // 다국어의 경우
try {
// URL
URL url = new URL("https://m.daum.net");
br = new BufferedReader(new InputStreamReader(url.openStream()));
// I/O
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} finally {
if(br != null) try {br.close();} catch(IOException e) {}
}
}
=> 해당 페이지의 소스를 확인할 수 있다.
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
// URL
URL url = new URL("https://news.daum.net");
br = new BufferedReader(new InputStreamReader(url.openStream()));
// I/O
String line = "";
boolean flag = false;
while((line = br.readLine()) != null) {
/*내용을 뽑아낼 때 사용할 수 있는 명령어
* equals
* contains / startWith / endsWith
* indexOf
*/
// 뽑아낼 내용
// <ul class="list_newsissue">
// </ul>
// class="link_txt" data-tiara-layer="article_main"
// </a>
if(line.contains("class=\"link_txt\" data-tiara-layer=\"article_main\"")) {
flag = true;
}
if(line.contains("</a>")) {
flag = false;
}
if(flag) {
System.out.println(line);
// br.readLine() 한 줄 건너 읽기 / br.readLine().trim() 공백제거
}
}
System.out.println();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} finally {
if(br != null) try {br.close();} catch(IOException e) {}
}
}
System.out.println(br.readLine()); 일 때
System.out.println(br.readLine().trim()); 일 때
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
try {
//URLConnection conn = new URL("https://news.daum.net").openConnection();
HttpsURLConnection conn = (HttpsURLConnection)new URL("https://news.daum.net/").openConnection();
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 에러코드를 잡아낼 때 사용
int responseCode = conn.getResponseCode();
System.out.println(responseCode);
// 에러코드 심화
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println(conn.getRequestMethod());
System.out.println(conn.getResponseMessage());;
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}else {
System.out.println("접속에러");
}
/*String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}*/
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[에러] : " + e.getMessage());
} finally {
if(br != null) try {br.close();} catch(IOException e) {}
}
}
// bufferedInput/outputstream
BufferedInputStream bis = null; // 이미지 읽기
BufferedOutputStream bos = null; // 이미지 쓰기(저장)
try {
HttpURLConnection conn = (HttpURLConnection)new URL("https://t1.daumcdn.net/daumtop_chanel/op/20200723055344399.png").openConnection();
bis = new BufferedInputStream(conn.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("./daum.png"));
int data = 0;
while((data = bis.read()) != -1) {
bos.write(data);
}
System.out.println("다운로드 완료");
} catch (MalformedURLException e) {
System.out.println("[에러] : " + e.getMessage());
} catch (IOException e) {
System.out.println("[에러] : " + e.getMessage());
} finally {
if(bos != null) try { bos.close(); } catch(IOException e) {}
if(bis != null) try { bis.close(); } catch(IOException e) {}
}
}
}
textField2.setText( new File("").getAbsolutePath().replaceAll( "\\\\", "/" ) + "/" ); // 경로 설정
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
HttpURLConnection conn =
(HttpURLConnection)new URL("https://search.pstatic.net/common/?src=http%3A%2F%2F
imgnews.naver.net%2Fimage%2F213%2F2022%2F05%2F03%2F0001215166_001_20220503151503059.
jpg&type=a340").openConnection(); // 이미지 연결
bis = new BufferedInputStream(conn.getInputStream()); // 읽기
//bos = new BufferedOutputStream(new FileOutputStream("./박효신.jpg"));
만든 후 주석처리
int data = 0;
while((data = bis.read()) != -1) {
//bos.write(data); // 만든 후 주석처리
lbl1.setIcon( new ImageIcon( "./sohee.jpg" ) ); // 라벨에 이미지 보이기
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
if(bos != null) try { bos.close(); } catch(IOException e1) {}
if(bis != null) try { bis.close(); } catch(IOException e1) {}
}
}
});
HTML 페이지를 웹브라우저가 사용하는 DOM(Document Object Model) 형식으로 변경
https://jsoup.org/ - > java버전의 Html분석
public static void main(String[] args) {
// TODO Auto-generated method stub
String html = "<html>"
+ "<head><title>First parse</title></head>"
+ "<body>"
+ "<p>Parsed HTML into a doc.</p>"
+ "<p>Parsed HTML into a doc.</p>"
+ "</body>"
+ "</html>";
Document doc = Jsoup.parse(html); // DOM 구조로 정렬함
/* //System.out.println(doc);
* System.out.println(doc.title()); // 타이틀 출력
* Elements titles = doc.getElementsByTag("title");
* System.out.println(titles); // html 기능 + 타이틀 출력
* System.out.println(titles.text()); // 타이틀 출력
*/
Elements pTags = doc.getElementsByTag("p");
// System.out.println(pTags);
for(int i = 0; i < pTags.size(); i++) {
Element pTag = pTags.get(i);
System.out.println(pTag); // 기능 + 텍스트 출력
System.out.println(pTag.tagName()); // 기능이름만 출력
System.out.println(pTag.text()); // 텍스트만 출력
}
}
}
id / class로 정보 출력
public static void main(String[] args) {
// TODO Auto-generated method stub
String html = "<html>"
+ "<head><title>First parse</title></head>"
+ "<body>"
+ "<p id='i1' class = 'c1'>Parsed HTML into a doc 1.</p>"
+ "<p id='i2' class = 'c2'>Parsed HTML into a doc 2.</p>"
+ "<p id='i3' class = 'c1'>Parsed HTML into a doc 3.</p>"
+ "<p id='i4' class = 'c2'>Parsed HTML into a doc 4.</p>"
+ "</body>"
+ "</html>";
Document doc = Jsoup.parse(html); // DOM 구조로 정렬
// id에 대한 정보
Element pTag = doc.getElementById("i1");
System.out.println(pTag);
System.out.println();
// class에 대한 정보
Elements pTags = doc.getElementsByClass("c1");
System.out.println(pTags);
}
}
select / css 값 출력
public static void main(String[] args) {
// TODO Auto-generated method stub
String html = "<html>"
+ "<head><title>First parse</title></head>"
+ "<body>"
+ "<a href = 'https://www.daum.net'>"
+ "<img class ='c1' src ='./daum.jpg'>"
+ "<div id = 'i1'>다음바로가기</div>"
+ "</a>"
+ "<a href = 'https://www.naver.com'>"
+ "<img class ='c1' src ='./naver.jpg'>"
+ "<div id = 'i2'>네이버바로가기</div>"
+ "</a>"
+ "<a href = 'https://www.google.com>"
+ "<img class ='c1's src ='./google.jpg'>"
+ "<div id = 'i3'>구글바로가기</div>"
+ "</a>"
+ "</body>"
+ "</html>";
Document doc = Jsoup.parse(html); // DOM 구조로 정렬
Elements divTags = doc.select("#i1"); // select 설정한 아이디 값 읽기
System.out.println(divTags.text());
Elements imgTags = doc.select("img.c1"); // class의 c1 값 가져오기
System.out.println((imgTags));
//속성 읽기
for(Element e : imgTags) { //c1이 여러개이므로 반복문
System.out.println(e.attr("src"));
}
//img에서 src가 jpg로 끝나는 값 가져오기
Elements imgTag2 = doc.select("img[src$=jpg]");
System.out.println(imgTag2);
}
}