개발일기/Spring

crawling - Jsoup

길동이이이잉 2024. 4. 10. 04:17
728x90
반응형

https://jsoup.org/cookbook/extracting-data/selector-syntax

 

Use CSS selectors to find elements: jsoup Java HTML parser

Use CSS selectors to find elements Problem You want to find or manipulate elements using CSS selectors. Solution Use the Element.select(String cssSelector) and Elements.select(String selector) methods: File input = new File("/tmp/input.html"); Document doc

jsoup.org

 

String crawlingURL = "";

File folder = new File("E:\\zinc\\");
folder.mkdir();

crawlingURL = "https://zinc.docking.org/substances/" + zinc + "/catitems/subsets/annotated/table.html";
File file = new File("E:\\zinc");
BufferedWriter errBw = new BufferedWriter(new FileWriter("E:\\zinc\\err.txt", true));

try{
		Document document = Jsoup.connect(crawlingURL).get();
		Elements pageCopy = document.getElementsByClass("table table-striped table-condensed table-hover");//class명 입력

		FileOutputStream fos = new FileOutputStream(new File(folder + "\\"+ zinc + ".txt"), true);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    BufferedWriter bw = new BufferedWriter(osw);

    bw.write(pageCopy.toString());

    bw.close();
    osw.close();
    fos.close();
} catch (Exception e){
		errBw.write(moleculeSource.getId() + ", " + zinc + "\n");
		errBw.flush();
		continue;
}
728x90
반응형