package sample.main.anli;
import com.jacob.com.ComThread;
import com.jacob.com.Variant;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import java.io.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLInputElement;
import javax.net.ssl.*;
public class getHtmlMain extends Application {
private static WebView webView;
private String finalS;
final int[] count = {1};
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("cases collector");
webView = new WebView();
TextField tf = new TextField();
Button bt = new Button("GO");
Button fresh = new Button("fresh");
Button start = new Button("start");
Text tt = new Text();
GridPane gp = new GridPane();
Scene scene = new Scene(gp, 960, 600);
gp.add(tf, 0, 0, 3, 1);
gp.add(webView, 0, 1, 3, 3);
gp.add(bt, 2, 5);
gp.add(fresh, 1, 5);
gp.add(start, 0, 5);
gp.add(tt, 0, 4);
fresh.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
webView.getEngine().load("https://00087919");
tf.setText("https://0087919");
}
});
bt.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
webView.getEngine().load(tf.getText());
}
});
start.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File("D:\\dayu\\contentid.txt");
FileReader fr = null;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);
String s = null;
Map<String,Integer> repeat=new HashMap<>();
while (true) {
try {
if (!((s = br.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
finalS = s;
if(null==repeat.get(s)){
repeat.put(finalS,0);
}else{
continue;
}
//key point here
Platform.runLater(new Runnable() {
@Override
public void run() {
tf.setText("https://owledge/" + finalS);
System.out.println("https://wledge/" + finalS);
webView.getEngine().load("https://wledge/" + finalS);
}
});
count[0]++;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
webView.getEngine().load("https://100087919");
tf.setText("https://0087919");
webView.getEngine().documentProperty().addListener(new ChangeListener<Document>() {
@Override
public void changed(ObservableValue<? extends Document> observable, Document oldValue, Document newValue) {
if(newValue!=null){
HTMLInputElement uid = (HTMLInputElement) newValue.getElementById("uid");
HTMLInputElement password = (HTMLInputElement) newValue.getElementById("password");
if (uid != null && password != null) {
uid.setValue("77");
password.setValue("!aa");
Document doc = webView.getEngine().getDocument();
NodeList inputNodeList = doc.getElementsByTagName("input");
for (int i = 0; i < inputNodeList.getLength(); i++) {
NamedNodeMap attrs = inputNodeList.item(i).getAttributes();
if ("button".equals(attrs.getNamedItem("type").getTextContent())) {
Node inputnode = inputNodeList.item(i);
((EventTarget) inputnode).addEventListener("click", new EventListener() {
@Override
public void handleEvent(Event evt) {
if (uid != null) {
try {
webView.getEngine().executeScript("submitForm()");
}catch (Exception e){
System.err.println(e.getMessage());
}
}
}
}, false);
HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
buttonThis.click();
}
}
}
}
String html = (String) webView.getEngine().executeScript("document.documentElement.outerHTML");
org.jsoup.nodes.Document doc1 = Jsoup.parse(html);
String title = doc1.getElementsByTag("h1").text();
// String title = doc1.getElementsByClass("knowledgebase-title").html();
title=title.replaceAll("\\\\","@");
title=title.replaceAll("/","@");
// 生成临时文件名称
String fileName = title=="" ? new Random().nextInt(5000)+"" : title + ".doc";
//todo 这里有bug,以后修改html中有多个h1的处理,如这个链接:https://xxx.com/enterprise/zh/kno/087246
System.out.println("title:"+title);
System.out.println("临时文件名:"+fileName);
String bodyStr = null;
bodyStr = doc1.getElementsByClass("case-content-box").html();
String content = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body><h3>" + title + "</h3>" + bodyStr + "</body></html>";
//将内容转成数组
byte b[] = new byte[0];
try {
b = content.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//使用的节点流
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
try {
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream ostream = null;
try {
fileName=fileName.replaceAll("\\\\","@");
fileName=fileName.replaceAll("/","@");
ostream = new FileOutputStream("D:\\dayu\\public3\\" + fileName);
} catch (FileNotFoundException e) {
System.err.println(tf.getText());
e.printStackTrace();
}
try {
poifs.writeFilesystem(ostream);
} catch (IOException e) {
e.printStackTrace();
}
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
primaryStage.setTitle(fileName);
title=title.replaceAll("\\\\","@");
title=title.replaceAll("/","@");
docToDOcx("D:\\dayu\\public3\\" + title + ".doc", "D:\\dayu\\public3\\" + title + ".docx");
tt.setText(count[0] + "/257");
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
launch(args);
}
public static void docToDOcx(String path, String outPath) {
com.spire.doc.Document dc = new com.spire.doc.Document();
dc.loadFromFile(path);
dc.saveToFile(outPath, FileFormat.Docx_2013);
dc.close();
}
}
05-11
1932
09-02