Web-Scraping

Web-Scraping(XML)

오늘 일하다가 웹스크랩핑 이란 것을 알게되었고,
간단히 테스트 프로그램을 만들어 보았다.

xml 페이지 읽어오기

package main;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class GetCurrencyInfo {

public static void main(String[] args) {
getCurRate();
}

private static void getCurRate() {
try {
// String searchUrl = "http://localhost/eurofxref-daily.xml";
String searchUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(searchUrl);

// xpath 생성
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/*/*/*/Cube";

NodeList cols = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);

System.out.println("=====================");
System.out.println(cols.item(0).getParentNode().getAttributes().getNamedItem("time").getNodeValue());
System.out.println("=====================");

for (int idx = 0; idx < cols.getLength(); idx++) {
String cur_rate = cols.item(idx).getAttributes().getNamedItem("currency").getNodeValue() + " " + cols.item(idx).getAttributes().getNamedItem("rate").getNodeValue();
System.out.println(cur_rate);
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

구동결과

runnable jar 로 만들어서 돌려본 결과…

D:\dev_git\TestRunnable>java -jar CheckCur_local.jar
=====================
2017-04-05
=====================
USD 1.0678
JPY 118.49
BGN 1.9558
CZK 27.058
DKK 7.4354
...

[참고]
[ksah_web scraping]1
[xPath_doc_tutorial]2 


'프로그래밍 > JAVA' 카테고리의 다른 글

Java Pair 자료구조  (0) 2017.04.14
블로그 이미지

ohnewdev

배워서 남주자

,