0

i have written a python script to goto a website and scrape some text off the website and save that text into a text file on my computer

from selenium import webdriver
import os

chrome_path = r"C:\tf_alert\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.minuteinbox.com/")

email = driver.find_element_by_xpath("""/html/body/div[2]/div[3]/div[1]/div[3]/div/span""").text
strEmail = str(email)
mailMan = open("10MAIL.txt", "a")
mailMan.write(strEmail)
mailMan.close()

os.system("taskkill /im py.exe")

when i run the script from where it is located on my computer all works fine and text is properly written to text file on my computer

but when i try to integrate the python script into a java program (code below), the text scraped off of the website does not get written to text file

public void SimpleTest() throws InterruptedException, IOException {

    Desktop desktop01 = Desktop.getDesktop();
    File file01 = new File("C:\\tf_alert\\other python projects\\mailMan.py");
    if (file01.exists()) {
        desktop01.open(file01);
    }
    Thread.sleep(20000);

    StringBuilder contentBuilder01 = new StringBuilder();
    try (Stream<String> stream = Files.lines(Paths.get("C:\\tf_alert\\other python projects\\10MAIL.txt"), StandardCharsets.UTF_8)) {
        stream.forEach(s -> contentBuilder01.append(s).append(""));
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(contentBuilder01);

    PrintWriter pw = new PrintWriter("C:\\tf_alert\\other python projects\\10MAIL.txt");
    pw.close();
}

the purpose of the java program above is to activate the python script to scrape text off a website, then get the text written from the python script from the file on my computer, print the text to console, and then clear the text file so that all is ready for next execution

the problem is occuring during the writing of text mailMan.write(strEmail) in python code because when java prints the text from the text file it prints a blank ""

i suppose there is interference between java and python

can anyone help out?

azro
  • 53,056
  • 7
  • 34
  • 70
dam1ne
  • 181
  • 1
  • 2
  • 17

2 Answers2

0

You are not executing the python script.

Running a .py file from Java should help you. Another source: Three ways to run Python programs from Java

lireoy
  • 36
  • 2
0

to fix this problem i created a windows batch file that changes directory to where the python file is located and starts that file

cd C:\your-directory

start yourscript.py
dam1ne
  • 181
  • 1
  • 2
  • 17