之前学习swing时写个一个简单的汇率转换器,之前又学习了python,于是想用python写个爬虫,把数据给java展示出来,媒介采用jython,将python解析的结果保存到txt文档中,java读取文档 ,如果不用jython就得手动先运行一遍python,就没意思了,等于只用运行java程序,每一次运行时通过jython自动调用java程序.
python程序:(爬取数据,正则匹配,结果保存到e:1.txt)
import urllib
import urllib2
import re
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
url="http://www.boc.cn/sourcedb/whpj/";
spath="E://1.txt"
f=open(spath,"w")
user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
headers = { 'User-Agent' : user_agent}
request=urllib2.Request(url,None,headers);
response =urllib2.urlopen(request)
content=response.read().decode("utf-8")
#f.write(content)
result = re.findall('(?<=).+?(?= )',content)
f.write(result[178]+"\n")
#f.write(cj)
f.close()
1.http://sourceforge.net/projects/jython/下载jython包
2 .按照,http://jingyan.baidu.com/article/b24f6c822a5abb86bfe5dae4.html的方法安装jython
3.安装完毕后,将新的安装目录下的jython.jar加载到eclipse http://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html
4可以使用了...
java代码:
import java.awt.*;
import javax.script.*;
import org.python.util.PythonInterpreter; //引入jython
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import java.io.*;
import static java.lang.System.*;
public class device extends JFrame{
private double d;
public class alert extends JDialog{
public alert(){
setTitle("警告");
setLayout(null);
setBounds(400,300,250,200);
Container container=getContentPane();
JLabel jl= new JLabel("拜托请输入正确的兑换金额");
jl.setBounds(50,50,200,100);
container.add(jl);
container.setBackground(Color.white);
setVisible(true);
}
}
public device()throws IOException{
Process proc = Runtime.getRuntime().exec("python C:\\Python27\\c.py");
try {
proc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setTitle("汇率兑换器");
setLayout(null);
setBounds(200,200,500,550);//第一个为窗口左上点的横坐标,第二个为窗口左上点的纵坐标,第三个为窗口横向大小,第四个为纵向窗口
Container container=getContentPane();
JLabel jl= new JLabel("兑换金额(美元)");
jl.setBounds(50,150,200,100);//相对于窗口
container.add(jl);
JLabel jl2=new JLabel("兑换金额(人民币)");
jl2.setBounds(45,250,200,100);
container.add(jl2);
JTextField jt=new JTextField("");
jt.setBounds(175,170,200,50);
container.add(jt);
JTextField jt2=new JTextField("");
jt2.setBounds(175,275,200,50);
container.add(jt2);
JButton jb=new JButton("重置");
jb.setBounds(100,400,100,50);
container.add(jb);
JButton jb2=new JButton("兑换");
jb2.setBounds(250,400,100,50);
container.add(jb2);
FileReader fin = new FileReader("E:\\1.txt");//读入文件
BufferedReader bin = new BufferedReader(fin);
String str=bin.readLine();//读入
bin.close();//关闭文件
if (str.isEmpty()){
System.out.println("没有联网\n");
}
d=Double.valueOf(str);
d=d/100;
JLabel jl3=new JLabel("当前汇率(每百美元)为:");
jl3.setBounds(30,50,200,100);
container.add(jl3);
JTextField jt3=new JTextField(str);
jt3.setBounds(175,75,200,50);
container.add(jt3);
container.setBackground(Color.white);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stu9b
jt.setText("");
jt2.setText("");
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {//要判断输入是否合法
// TODO Auto-generated method stu9b
String s= jt.getText();
if ((s.matches("\\d+"))||(s.matches("\\d+\\.\\d+"))){
double n=Double.parseDouble(s);
n=n*d;
String ss=new java.text.DecimalFormat("#.000").format(n);//防止出现浮点数异常(比如1.000005)
jt2.setText(ss);
}
else {
new alert();
jt.setText("");
}
}
});
}
public static void main(String args[]){
try {
new device();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上为完整代码:
github(注意修改读取的文件名和python的文件路径) https://github.com/fengsigaoju/Currency-converter-