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())

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

1个回答

process = Runtime.getRuntime().exec(cmdArr,null,new File(path));
看看这里的路径对不对

qq_36792042
Yanareat 对呀!我调用python的其他代码都可行,就是这个scrapy框架的爬虫不行
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问