我是一名投资者,希望利用某些股票变量在sql数据库中进行分析(特别是市盈率)。
我阅读了一本基本的Java书籍,但目前缺乏编程经验来从网页上获取数据并将其存储在表格中。我查看了HtmlUnit页面上的示例,但不知道如何使一切正常工作。
目标:
这是我完成任务的基本尝试。
// First, I need to import some Htmlunit libraries, but im not sure which ones yet.
// Code below datamines the PE ratio for aapl?
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://ycharts.com/companies/AAPL/historical_data/pe_ratio");
HtmlTable table = (HtmlTable)page.getFirstByXPath("//table[@class='histDataTable']");
for (HtmlTableRow row : table.getRows()) {
System.out.println("Found row");
for (HtmlTableCell cell : row.getCells()) {
System.out.println(" Found cell: " + cell.asText());
// Not familiar enough with exceptions, but think i need them somewhere?
// And then when I decide to actually create and update statements, ill need something like?
System.out.println("doing ticker " + ticker + " " + date + " " + pe);
insert.setString(1,ticker);
insert.setDate(2,date);
insert.setDouble(3,pe);
insert.execute();
}catch(PSQLException e)
{
//set update values
update.execute();
}