java调用scrapy无反应

java调用scrapy无反应

 String writeFilePath="";
        String path="A:\\0_document\\pythonWorkSpace\\ysu\\ysu";
//        String path="crawl ";
        //python脚本的路径
        String executePath = path + "\\main.py";
//        String executePath = path + "ysu_spider";
        //执行命令Arr
        System.out.println(executePath);
//        System.out.println(System.getProperty("user.dir"));
        String[] cmdArr = new String[]{"python",executePath};
//        String[] cmdArr = new String[]{"scrapy",executePath};
        //参数分别为: 执行命令;执行此脚本的路径
        Process process = null;
        try {
            System.out.println("1");
            process = Runtime.getRuntime().exec(cmdArr,null,new File(path));
            System.out.println(process);
            InputStream inputStream = process.getInputStream();
            System.out.println(inputStream);
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
                writeFilePath = new String(b);
                System.out.println(writeFilePath);
            }
//            返回linux执行状态码,0为执行正常
            int statusNum = process.waitFor();
            System.out.println(statusNum);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
//        }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

控制台:

A:\0_document\pythonWorkSpace\ysu\ysu\main.py
1
java.lang.ProcessImpl@681a9515
java.io.BufferedInputStream@3af49f1c

main.py

from scrapy import cmdline
import os
if __name__ == '__main__':
    os.chdir("A:/0_document/pythonWorkSpace/ysu/ysu");
    print(1+2)
    cmdline.execute('scrapy crawl ysu_spider'.split())

到底怎么回事呀?救救孩子!


小憩兰
浏览 1763回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP