程序终止,没有任何错误

时间:2014-04-17 06:35:36

标签: java python windows yelp

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

问题: 当我从Commandline运行这个程序时: Python msp.py dennys-san-jose-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(),但没有任何反应。

请建议。

谢谢。

3 个答案:

答案 0 :(得分:1)

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脚本实际上是 /yelp吗?

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

最后,检查Java程序运行时您所在的目录。如果它不是您所期望的,您可能会在一个完全出乎意料的地方创建reviews.txt

答案 1 :(得分:0)

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并设置路径变量

答案 2 :(得分:0)

我已经设法找到了这个解决方案,尽管我还没有得到它。

我使用相同的代码,它适用于Ubuntu。

cmd[0] = "python";

这是我修改过的所有内容,我运行了相同的脚本和b00m,它可以正常工作!

问题是为什么?

相关问题