java调用scrapy无反应解决办法

在第一次执行这串代码时,无任何反应。
之后尝试发现:遇到这个问题时,本地的python环境我用的是annaconda配置的,在重新更改配置了本地python环境(从python官网下载并配置好的环境、安装相应的库)后,java程序就能正常调用scrapy,并返回想要的结果。

import java.io.*;
public class StartSpider {

    private static String path = "F:\\Pychram\\Project\\mySpider";

    public static void main(String[] args) throws InterruptedException, IOException {
        //python脚本的路径
        String executePath = path + "\\startSpiders.py";
        //执行命令Arr
        String[] cmdArr = new String[]{"python", executePath};
        //参数分别为: 执行命令;执行此脚本的路径
        Process process = Runtime.getRuntime().exec(cmdArr, null, new File(path));
        //BufferedReader这段代码中的GBK是为了防止Python输出中文时乱码
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
        //接收scrapy中的print打印:
        InputStream inputStream = process.getInputStream();
        byte[] b = new byte[1024];
        while (inputStream.read(b) != -1) {
            String writeFilePath = new String(b);
        }
        //返回linux执行状态码,0为执行正常
        int statusNum = process.waitFor();
        inputStream.close();
    }
}

你可能感兴趣的:(爬虫,笔记,scrapy)