2
$client = new Client();
$a = 'https://scholar.google.com/citations?user=';
$gscID = 'EnegzCwAAAAJ';//for eg
$b = '&hl=en&oi=ao';
$url = $a . $gscID . $b;

$crawler = $client->request('GET', $url);

//python script
$process = new Process(['python', public_path() . '\ext.py'], null, ['SYSTEMROOT' => getenv('SYSTEMROOT'), 'PATH' => getenv("PATH")]);
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}
$out[] = $process->getOutput();
dd($out);

Extracting the tables from the given url webpage

Now I want to pass the $url variable to the python script stored in my public folder because everytime my url changes with different person's ID.

Any help would be really appreciated

3 Answers 3

3

Found out an easy solution $output = shell_exec("python ext.py $url");//ext.py is name of my script and $url is my variable which I want to pass

And then in python script write

import sys
url = sys.argv[1]
Sign up to request clarification or add additional context in comments.

Comments

1

Laravel's documentation recommends the use of env method mentioned here. I had to use a bash script to activate the python environment first (2nd step)

  1. First call the process in the job controller with the env method where you can pass all your variables as an array.
$scriptPath = base_path('python/run.sh');

$process = Process::env([
  'APP_URL' => config('app.url'),
  'OUTPUT_FILE' => $outputFile,
])->run('sh ' . $scriptPath);
  1. In the python/run.sh file I
#!/bin/bash

# Navigate to the script's directory
cd "${0%/*}"

# Activate the virtual environment
source venv/bin/activate

# Run the Python script with the output file argument
python export.py --output_file "$OUTPUT_FILE" --app_url "$APP_URL"

# Deactivate the virtual environment
deactivate

  1. The export.py parses the arguments with the help of argparse library
import argparse

if __name__ == "__main__":
    # Save the file to the path provided as an argument

    parser = argparse.ArgumentParser(description='Export products to Excel.')
    parser.add_argument('--output_file', type=str, required=True, help='The output file path')
    parser.add_argument('--app_url', type=str, required=True, help='The application URL')

    args = parser.parse_args()

    export_products_to_excel(output_file=args.output_file,app_url=args.app_url)

Comments

0

You can add script parameters in the array that you are passing to Process. Here is the doc.https://symfony.com/doc/current/components/process.html#using-features-from-the-os-shell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.