【发布时间】:2021-03-10 22:23:41
【问题描述】:
我想自动进行文件转换: https://www.gpsvisualizer.com/map_input?form=googleearth。 我的问题是,gpsvisualizer 允许独立转换,但我有 500 个文件要转换。 所以我使用 hmtlUnit 来自动化这个过程。
感谢以下代码,我可以修改“选择”,例如:
- “输出文件类型”
- “添加 DEM 高程数据”
上传我的文件并获取重定向的 html 页面的 url,我可以在其中下载想要的文件。
我的问题是我找不到下载文件的方法。
有人有建议吗?
提前谢谢。
这是我的代码:
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setRedirectEnabled(true);
//fetching the web page
String url = "https://www.gpsvisualizer.com/map_input?form=googleearth";
//String url = "https://www.reddit.com/r/scraping/";
HtmlPage page = webClient.getPage(url);
System.out.println(page.getUrl());
System.out.println(page.getTitleText());
//Select set .kml file
HtmlSelect selectFileType = (HtmlSelect) page.getElementByName("googleearth_zip");
System.out.println(selectFileType.getOption(0).asText());
//System.out.println(selectFileType.getOption(1).asText());
HtmlOption kmlFile = selectFileType.getOptionByText(".kml (uncompressed)");
System.out.println(kmlFile.asText());
selectFileType.setSelectedAttribute(kmlFile, true);
//Select add elevation on file
HtmlSelect selectelevation = (HtmlSelect) page.getElementByName("add_elevation");
System.out.println(selectelevation.getOption(4).asText());
HtmlOption europeSRTM1 = selectelevation.getOptionByText("NASA SRTM1 (30m res., NoAm, Europe, more)");
System.out.println(europeSRTM1.asText());
selectelevation.setSelectedAttribute(europeSRTM1, true);
//add file
HtmlForm myForm = page.getFormByName("main");
HtmlFileInput fileInput = myForm.getInputByName("uploaded_file_1");
fileInput.setValueAttribute("/media/Stock/Projets/Suratram/Ressources/Traces_WS/puissance/kml_files/01_douce-signoret.kml");
HtmlElement submitBtn = page.getElementByName("submitted");
//page google
HtmlPage page2 = submitBtn.click();
System.out.println(page2.getUrl());
【问题讨论】:
标签: java web web-scraping htmlunit