2014-04-17 85 views
1

我想在Java中运行这个Python程序。程序终止没有任何错误

问题: 当我从命令行运行此程序:Python的msp.py丹尼斯 - 圣 - 何塞 - 2 - 它WORKS

当我通过这个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 

问题(简言之):

当我运行通过命令行这个脚本,它的工作原理和它终于商店a reviews.txt文件。但是当我通过这个程序运行它时,什么也没有发生

我玩过pr.wait()和pr.waitfor(),但没有任何反应。

请指教。

谢谢。

回答

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

+0

你能帮我解决吗?我只是想通过该字符串作为参数。我如何修改这个脚本以适合我的需求?我正在通过“dennys-san-jose-2”或类似的东西。 –

+1

@Rocky,我给答案增加了两种可能性,一种是如果你只想要一个参数,另一种是如果你想传递一个字符串数组。 – paxdiablo

+0

男人你是一个救星! 'SO'岩石!我会检查并通知你! –

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

+0

我想运行这个,但是当我使用/yelp/msp.py时,脚本本身给了我一个错误。我可以知道我当前的错在哪里吗?因为它适用于所有其他的Python脚本 –

+0

你正在得到什么错误?你能提供日志吗? –

0

我设法找到了这个问题的解决,虽然我还是不明白这一点。

我使用了相同的代码,它在Ubuntu上工作。

cmd[0] = "python"; 

这是我修改,我跑了相同的脚本和b00m和它的作品!

问题是为什么?