我正在尝试用 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() 但没有任何反应。
请指教。
谢谢你。