繁体   English   中英

程序终止,没有任何错误

[英]Program terminates without any error

我正在尝试用Java运行此Python程序。

问题:当我运行命令行从这个程序:Python的msp.py丹尼斯-圣-何塞- 2 -它的工作原理

当我通过此java程序调用相同的脚本时。 它只是终止。 我测试了其他python脚本,它们起作用了!

public void pythonrun(String args) throws IOException, InterruptedException
{
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
    cmd[i+2] = args;
    }

    // create runtime to execute external command
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(cmd);
    //pr.waitFor();

    // retrieve output from python script
    BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = "";
    while((line = bfr.readLine()) != null) {
    // display each output line form python script
    System.out.println(line);
    }
}

public static void main(String args[]) throws IOException, InterruptedException {
YelpPython demo = new YelpPython();
demo.pythonrun("dennys-san-jose-2");
}

脚本(msp.py):

它能做什么? (简而言之,脚本转到页面并刮取评论)

from bs4 import BeautifulSoup
from urllib import urlopen
import sys
queries = 0
while queries <201:
    stringQ = sys.argv[1]
    page = urlopen('http://www.yelp.com/biz/' + stringQ)

    soup = BeautifulSoup(page)
    reviews = soup.findAll('p', attrs={'itemprop':'description'})
    authors = soup.findAll('span', attrs={'itemprop':'author'})

    flag = True
    indexOf = 1

    for review in reviews:
        dirtyEntry = str(review)
        while dirtyEntry.index('<') != -1:
            indexOf = dirtyEntry.index('<')
            endOf = dirtyEntry.index('>')
            if flag:
                dirtyEntry = dirtyEntry[endOf+1:]
                flag = False
            else:
                if(endOf+1 == len(dirtyEntry)):
                    cleanEntry = dirtyEntry[0:indexOf]
                    break
                else:
                    dirtyEntry = dirtyEntry[0:indexOf]+dirtyEntry[endOf+1:]
        f=open("reviews.txt", "a")
        f.write(cleanEntry)
        f.write("\n")
        f.close
    queries = queries + 40

问题(总而言之):

当我通过命令行运行此脚本时,它可以工作,并且最终存储了reviews.txt文件。 但是当我通过该程序运行它时,什么也没有发生。

我玩过pr.wait()和pr.waitfor(),但是什么也没发生。

请指教。

谢谢。

public void pythonrun(String args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
        cmd[i+2] = args;
    }
    :
}

看起来不太正确。 您正在创建一个字符串数组,其大小取决于要传入的字符串的大小。

这意味着pythonrun("1234")将最终执行:

C:\Python27\python.exe /yel/msp.py 1234 1234 1234 1234

如果只想为脚本传递一个参数,则可以执行以下操作:

public void pythonrun(String args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[3];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    cmd[2] = args;
    :
}

如果您想传递参数数组 ,则如下所示会更好:

public void pythonrun(String [] args) throws IOException, InterruptedException {
    String pythonScriptPath = "/yelp/msp.py";
    String[] cmd = new String[2 + args.length()];
    cmd[0] = "C:\\Python27\\python.exe";
    cmd[1] = pythonScriptPath;
    for(int i = 0; i < args.length(); i++) {
        cmd[i+2] = args[i];
    }
    :
}

通过将以下代码放置在设置它们的位置之后,您可以准确地确定该过程正在使用哪些参数:

for (int i = 0; i < cmd.length(); i++)
    System.out.println ("DEBUG " + i + ": [" + cmd[i] + "]");

除此之外,您的命令行版本与从Java程序调用的版本之间可能会有差异。

一方面,您的Java程序正在调用/yelp/msp.py而您的命令行版本msp.py直接调用msp.py 您确定您的msp.py脚本实际上 /yelp msp.py 吗?

还要确保C:\\Python27\\python.exe是正确的Python解释器。

最后,检查Java程序运行时所处的目录。 如果这不是您所期望的,那么您很可能在一个完全意外的地方创建reviews.txt

String python_script_path ="Path to Python script/";
ProcessBuilder pb = new ProcessBuilder("python","msp.py","parameter1","parameter2");
 pb.directory(new File(python_script_path));
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br1 = new BufferedReader(isr);
String line1;
 while ((line1 = br1.readLine()) != null) {
 System.out.println(line1);
 }

使用Processbuilder并设置路径变量

尽管我还是不明白,但我设法找到了解决方案。

我使用了相同的代码,并且可以在Ubuntu上运行。

cmd[0] = "python";

这就是我所做的全部修改,并且我运行了相同的脚本和b00m,并且可以正常工作!

问题是为什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM