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 = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(searchUrl);
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