前言:
如今兄弟们对“股票行情html”都比较讲究,我们都需要剖析一些“股票行情html”的相关内容。那么小编也在网上收集了一些对于“股票行情html””的相关资讯,希望咱们能喜欢,兄弟们快快来了解一下吧!各位老总,有时我们想钱想疯了(没办法,上有老,下有小),就去玩股票,虽然大多数当菲菜,让人割了,但还是乐此不疲。
上APP、上软件看各股资料太麻烦?不好找出各股的对比数据?那你可以把各股资料放入数据库,像平常我们玩数据库一样,想查哪个市值多少,哪个地方的,非常方便。
下面程序是我之前写的读取各股票信息,然后存到数据库里,供自己分析,大家可以参考,编写更详细的信息。
主要是读取东方财富网的数据
上证各股资料的URL都是:股票编号.html
深上证各股资料的URL都是:股票编号.html
代码如下(几年前的JAVA代码,能运行就得了)
package yjz.nms.cla;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetStockInfo {
public void getData() throws IOException {
//股票名
String stockname=null ;
//板块
String bankuai=null;
//业务范围
String yewufanwei=null ;
//概念
String gainian=null ;
//发行价
float faxingjia;
//注册资本
float zhuceziben = 0 ;
//市值
float shizhi ;
//市盈率
float shiyinglv = 0 ;
//每股收益
float meigushouyi = 0 ;
//业务收入
float yingyeshouru = 0 ;
//收入同比
float shourutongbi ;
//利润
float liren = 0 ;
//利润同比
float lirentongbi ;
//市净率
float shijinglv ;
Connection con = null;
// 连接数据库
con = connDB(con);
String codeSqe=null;
codeSqe=getCodeSeq(con );
String fileNameN = "e:\\allStockCode.ini";
File f = new File(fileNameN);
String data = null;
if (f.exists()) {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileNameN), "UTF-8"));
while((data=br.readLine())!=null && ! data.equals("")){
data=data.substring(data.indexOf("(")+1,data.indexOf(")"));
String urls = null;
if (data.startsWith("60")) {
urls = ""+data+".html";
} else {
urls = ""+data+".html";
}
String htmlContent= getHtmlContent(urls,"gb2312");
stockname = getContent(htmlContent,"<h2 class=\"header-title-h2 fl\" id=\"name\">(.*?)</h2>");
shizhi = Float.parseFloat(getContent(htmlContent,"<span id=\"gt7_2\">(.*?)亿</span>"));
String tmp=getContent(htmlContent,"<span id=\"gt6_2\">(.*?)</span>");
if(tmp.indexOf("-")<0){
shiyinglv = Float.parseFloat(tmp);
}
tmp=getContent(htmlContent,"收益</a>.*(\\d+\\.\\d+?)</td>");
if(tmp.indexOf("-")<0){
meigushouyi = Float.parseFloat(tmp);
}
String yingyeshourus = getContent(htmlContent,"<td>收入:(.*?)</td>");
if(yingyeshourus.indexOf("亿")>0){
yingyeshouru=Float.parseFloat(yingyeshourus.substring(0,yingyeshourus.indexOf("亿")))*10000;
}else{
yingyeshouru=Float.parseFloat(yingyeshourus.substring(0,yingyeshourus.indexOf("万")));
}
shourutongbi = Float.parseFloat(getContent(htmlContent,"同比</a>:(.*?)%</td>"));
String lirens = getContent(htmlContent,"<td>净利润:(.*?)</td>");
if(lirens.indexOf("亿")>0){
liren=Float.parseFloat(lirens.substring(0,lirens.indexOf("亿")))*10000;
}else{
tmp=lirens.substring(0,lirens.indexOf("万"));
liren=Float.parseFloat(tmp);
}
lirentongbi = Float.parseFloat(getContent(htmlContent,"<td>同比:(.*?)%</td>"));
shijinglv = Float.parseFloat(getContent(htmlContent,"<td>市净率:<span id=\"gt13_2\">(.*?)</span></td>"));
if (data.startsWith("60")) {
urls = ""+data;
} else {
urls = ""+data;
}
htmlContent= getHtmlContent(urls,"utf-8");
yewufanwei = getContent(htmlContent,"<th class=\"tips-fieldnameL\">经营范围</th><td colspan=\"3\" class=\"tips-dataL tips-lineheight\" width=\"782\">(.*?)</td>");
bankuai = getContent(htmlContent,"<th class=\"tips-fieldnameL\">所属行业</th><td class=\"tips-dataL\">(.*?)</td></tr>");
String zhucezibens = getContent(htmlContent,"注册资本\\(元\\)</th><td class=\"tips-dataL\">(.*?)</td>");
if(zhucezibens.indexOf("亿")>0){
zhuceziben=Float.parseFloat(zhucezibens.substring(0,zhucezibens.indexOf("亿")))*10000;
}else{
zhuceziben=Float.parseFloat(zhucezibens.substring(0,zhucezibens.indexOf("万")));
}
System.out.println(getContent(htmlContent,"每股发行价\\(元\\)</th><td class=\"tips-dataL\">(.*?)</td>"));
faxingjia= Float.parseFloat(getContent(htmlContent,"每股发行价\\(元\\)</th><td class=\"tips-dataL\">(.*?)</td>"));
if (data.startsWith("60")) {
urls = ""+data;
} else {
urls = ""+data;
}
htmlContent= getHtmlContent(urls,"utf-8");
gainian= getContent(htmlContent,"<p>要点一:<font>所属板块</font> (.*?)</p>");
/*System.out.println("data:"+data);
System.out.println("stockname:"+stockname);
System.out.println("bankuai:"+bankuai);
System.out.println("yewufanwei:"+yewufanwei);
System.out.println("gainian:"+gainian);;
System.out.println("faxingjia:"+faxingjia);
System.out.println("zhuceziben:"+zhuceziben);
System.out.println("shizhi:"+shizhi);
System.out.println("shiyinglv:"+shiyinglv );
System.out.println("meigushouyi:"+meigushouyi );
System.out.println("yingyeshouru:"+yingyeshouru);
System.out.println("shourutongbi:"+shourutongbi);
System.out.println("liren:"+liren );
System.out.println("lirentongbi:"+lirentongbi);
System.out.println("shijinglv:"+shijinglv);*/
if((codeSqe==null) || (!codeSqe.contains(data))){
try {
String insertsql = "insert into stockINFO(stockcode , stockname, bankuai, yewufanwei,gainian ,faxingjia ,zhuceziben ,shizhi,shiyinglv ,meigushouyi,yingyeshouru ,shourutongbi,liren,lirentongbi,shijinglv)" +
" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(insertsql);
pstmt.setString(1, data);
pstmt.setString(2, stockname);
pstmt.setString(3, bankuai);
pstmt.setString(4, yewufanwei);
pstmt.setString(5, gainian);
pstmt.setFloat(6, faxingjia);
pstmt.setFloat(7, zhuceziben);
pstmt.setFloat(8, shizhi);
pstmt.setFloat(9,shiyinglv);
pstmt.setFloat(10,meigushouyi );
pstmt.setFloat(11,yingyeshouru);
pstmt.setFloat(12,shourutongbi );
pstmt.setFloat(13,liren);
pstmt.setFloat(14,lirentongbi );
pstmt.setFloat(15,shijinglv );
int result = pstmt.executeUpdate();
if(result==1){
System.out.print("insert "+data+" into database ok");
}
pstmt.close();
} catch (SQLException e) {
System.out.println("SQL error"+data);
e.printStackTrace();
}catch (NumberFormatException e){
System.out.println("数安格式错"+data);
e.printStackTrace();
}
catch (StringIndexOutOfBoundsException e){
System.out.println("索引超出值"+data);
e.printStackTrace();
}
}else{
try {
String updatesql = "update stockINFO set stockname=?, bankuai=?, yewufanwei=?,gainian=? ,faxingjia=? ,zhuceziben=? ,shizhi=?,shiyinglv=? ,meigushouyi=?,yingyeshouru=? ,shourutongbi=?,liren=?,lirentongbi=?,shijinglv=? where stockcode=?" ;
PreparedStatement pstmt = con.prepareStatement(updatesql);
pstmt.setString(1, stockname);
pstmt.setString(2, bankuai);
pstmt.setString(3, yewufanwei);
pstmt.setString(4, gainian);
pstmt.setFloat(5, faxingjia);
pstmt.setFloat(6, zhuceziben);
pstmt.setFloat(7, shizhi);
pstmt.setFloat(8,shiyinglv);
pstmt.setFloat(9,meigushouyi );
pstmt.setFloat(10,yingyeshouru);
pstmt.setFloat(11,shourutongbi );
pstmt.setFloat(12,liren);
pstmt.setFloat(13,lirentongbi );
pstmt.setFloat(14,shijinglv );
pstmt.setString(15, data);
int result = pstmt.executeUpdate();
if(result==1){
System.out.print("update "+data+" into database ok");
}
pstmt.close();
} catch (SQLException e) {
System.out.println("SQL error"+data);
e.printStackTrace();
}catch (NumberFormatException e){
System.out.println("数安格式错"+data);
e.printStackTrace();
}
catch (StringIndexOutOfBoundsException e){
System.out.println("索引超出值"+data);
e.printStackTrace();
}
}
}
br.close();
br=null;
} else {
System.out.println("请在/slview/yjz,里面加上股票代码,用逗号分隔");
}
try {
con.close();
con=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Connection connDB(Connection con) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
System.out.println("Can't load dbdriver!-->"
+ e.getMessage());
}
try {
con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1639:dbnms",
"abc", "dddd");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return con;
}
public String getCodeSeq(Connection con ){
PreparedStatement pstmt=null;
ResultSet rs=null;
StringBuffer bs=new StringBuffer();
String sql = "select distinct stockcode from stockINFO" ;
try {
pstmt = con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
bs.append(rs.getString(1)+",");
}
}catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
rs=null;
pstmt.close();
pstmt=null;
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bs.toString();
}
public String getHtmlContent(String htmlURL,String code) {
URL url = null;
String rowContent = "";
StringBuffer htmlContent = new StringBuffer();
try {
url = new URL(htmlURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), code));
while ((rowContent = in.readLine()) != null) {
htmlContent.append(rowContent);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return htmlContent.toString();
}
public String getContent(String htmlContent,String regex){
StringBuffer mainContent = new StringBuffer();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(htmlContent);
if(matcher.find()) {
mainContent.append(matcher.group(1));
}
return mainContent.toString();
}
public static void main(String args[]) throws IOException,
InterruptedException {
GetStockInfo gsi=new GetStockInfo();
gsi.getData();
}
}
这个是只给大家一个参考,建议家里没有矿,还是少玩点。
标签: #股票行情html