1

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

问题: 当我从命令行运行此程序时: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() 但没有任何反应。

请指教。

谢谢你。

4

3 回答 3

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在一个完全出乎意料的地方进行创作。

于 2014-04-17T06:39:29.607 回答
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 并设置路径变量

于 2014-04-17T07:06:17.370 回答
0

尽管我仍然不明白,但我已经设法找到了解决方案。

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

cmd[0] = "python";

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

问题是为什么?

于 2014-04-18T09:03:53.770 回答