Java爬蟲獲取網頁表格數據

//Java爬蟲獲取網頁表格數據

public class Pachong implements Runnable {


public void run() {
String Rpt_date = null;
double price = 0;
//網頁地址
String url = "http://www.sse.net.cn/index/singleIndex?indexType=cbcfi";
try {
Document doc = Jsoup.connect(url).get();
//獲取第一個表格
Element element = doc.select("table").first();
// System.out.println(element);
Elements els = element.select("tr");
for (Element el : els) {
Elements ele = el.select("td");
for (Element elem : ele) {
if (elem.text().toString().indexOf("本期") != -1) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Rpt_date = elem.text().toString().substring(3);
}
if (elem.text().toString().equals("秦皇島-廣州(6-7萬DWT)")) {
price = Double.parseDouble(el.select("td").get(3).text().toString());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}


      //以下是將爬取到的數據保存到MySQL數據庫
InputStream in = GetOCFIAll.class.getClassLoader().getResourceAsStream("config.property");
String dbURL = "";
String userName = "";
String userPwd = "";
Properties properties = new Properties();
try {
properties.load(in);
dbURL = (String) properties.get("dburl");
userName = (String) properties.get("dbuser");
userPwd = (String) properties.get("dbpwd");
} catch (Exception e) {
e.printStackTrace();
}


try {
Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement statement = dbConn.createStatement();
String query = "SQL語句";
statement.addBatch(query);

statement.executeBatch();
statement.close();
dbConn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章