I have created a website that is written in HTML and PHP But for some of its features, I need to scrape some data from another website. So I have written script in Python for scraping purpose separately. Now I want to combine both Is it possible? How will it work? I need suggestions.
Asked
Active
Viewed 166 times
1 Answers
0
You can call python and ask it for executing python script and capture its output back to php an example:
<?php
$pyscript = 'G:\wamp\www\ll\test.py';
$python = 'C:\\Python27\\python.exe';
$cmd="$pyscript $python";
$result = exec("$cmd", $output);
Note that due to security reasons an exec() command will probably be turned off at shared hosting. But you should be able to use it on your own machine - local (computer) or hosted virtual machine. To check if you are able to use exec see this answer
more read on exec()
see other execution functions
For joining multiple lines of the output you can use implode
and if you need to return from python script different types of data (not only string) for example an array of found items then encode in python data to JSON and return it. On PHP side you can decode it with json_decode($data, true) that will convert JSON to a PHP array.

Jimmix
- 5,644
- 6
- 44
- 71