新浪股票网络接口ps:我写的第一个最简单的小爬虫o(^▽^)o

 1 package com.down.edu;
 2
 3 import java.io.BufferedReader;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.net.HttpURLConnection;
 7 import java.net.URL;
 8
 9 public class DOWNDATA {
10     public String data="";
11
12     public DOWNDATA(String code)
13     {
14         String Httpurl="http://hq.sinajs.cn/list=sh";
15         StringBuffer content=new StringBuffer();
16         try{
17             URL httpURL=new URL(Httpurl+code);
18             HttpURLConnection connection=(HttpURLConnection)httpURL.openConnection();
19             connection.connect();
20             InputStream is=connection.getInputStream();
21             BufferedReader br=new BufferedReader(new InputStreamReader(is));
22             String str="";
23             while((str=br.readLine())!=null){
24                 content.append(str);
25             }
26
27             br.close();
28             is.close();
29         }catch(Exception e){
30
31         }
32         String cont=content.toString();
33         data=cont.replace("var hq_str_sh"+code+"=\"", "").replace(",00\";", "");
34
35     }
36
37 }

这部分最主要,但也觉得没什么好讲的,也就一段以逗号隔开的字符,然后把不需要的部分删掉,最后在界面里呈现~请看:

package com.ui.edu;

import com.down.edu.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.down.edu.*;

public class UI implements ActionListener {

    /**
     * @param 独孤求败
     * 邮箱[email protected]
     * 功能:股票离线分析系统
     */

    JFrame f;
    JTextField jtf;
    JTextArea jta;
    JButton jb;
    JPanel jp;

    DefaultTableModel atm = null;
    Vector vDate = new Vector();
    Vector vName = new Vector();
    JTable table = null;
    JScrollPane sPane = null;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        UI ui = new UI();
    }

    public UI() {
        // 元素属性
        f = new JFrame("股票离线分析系统");
        f.setLayout(new BorderLayout());
        jp=new JPanel();
        jp.setLayout(new FlowLayout());

        jtf = new JTextField(40);
        jtf.addActionListener(this);

        vName.add("股票名字");
        vName.add("今日开盘价");
        vName.add("昨日收盘价");
        vName.add("当前价格");
        vName.add("今日最高价");
        vName.add("今日最低价");
        vName.add("竞买价");
        vName.add("竞卖价");
        vName.add("成交的股票数/100");
        vName.add("成交金额/万");
        atm = new DefaultTableModel(vDate, vName);
        Object[][] data = {};
        table = new JTable(atm);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//????
        table.setPreferredScrollableViewportSize(new Dimension(600, 80));
        Font x = new Font("seril", 0, 12);
        table.setFont(x);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.updateUI();
        jb = new JButton("提交");
        jb.addActionListener(this);
        // 添加元素
        f.getContentPane().add(scrollPane, BorderLayout.CENTER);
        jp.add(jtf);
        jp.add(jb);
        f.add(jp,BorderLayout.NORTH);

        // 框架设置
        f.setSize(1000, 600);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation((int) screenSize.getWidth() / 2 - 500,
                (int) screenSize.getHeight() / 2 - 300);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getActionCommand().equals("提交")) {
            System.out.println(jtf.getText());
            DOWNDATA downdata=new DOWNDATA(jtf.getText());
            addDate(downdata.data);
        }
    }
    public void addDate(String data) {
        Vector vTmp = new Vector();
        String split[]=data.split(",");
        for(int a=0;a<=9;a++)
        {
            System.out.println(split[a]);
            vTmp.add(split[a]);
        }
        vDate.add(vTmp);
        table.updateUI();
    }

}

总之很简单的东西了,给初学者一个借鉴吧,然后,万里长征第一步,I will be stronger~,一切都会好起来的,我不是在妥协生活,而是在等待黎明。

时间: 2024-12-07 20:54:53