How to run a Python Crawler with PHP

Asked

Viewed 430 times

2

I made a Crawler with Python and run it by command line:

python crawler.py

As soon as I execute this command he asks me for the keyword that will be searched and start running.

global keyword
keyword = raw_input('Keyword: ')

This data is used on an Laravel/PHP platform. But I would like to create a button like "Update" so that the user himself could run Crawler just by clicking on a button and entering the keyword.

  • It would be possible to script Python receive the keywork by command line argument and run it, in PHP, with the function exec? Something like exec("python crawler.py {$keyword}").

  • Why doesn’t Crawler in PHP?

  • I agree with @Guilhermenascimento, why force the use of something else and not do in Laravel with php?

  • @Lucaslopes answered, you can test.

  • Just out of curiosity, it is not better to already do Crawler in PHP, if it is to use in PHP?

  • @Andersoncarloswoss executed the command but it didn’t work. I put the $keyword = "urgente" and then ran the line you said, but it didn’t work.

  • @Lucaslopes you read http://answall.com/a/180360/3635 ?

  • @Lucaslopes had an error in the script, change fclose to pclose, I edited the answer.

  • Mate, your question is very confusing. It does not make it clear whether its intention of "button for the user to use" would be on a web page or a graphical user interface in Python. It is also unclear whether your difficulty is in integrating PHP with Python or using Python itself (direct user input versus receiving data via command line). Maybe she unintentionally takes the focus to the crowler (whose code and purpose are not even in the question, because maybe it’s not really important), when it seems to me that the focus should be on the integration between languages.

  • 1

    All the questions and assumptions ("if this... if that.. would not be better if...") you received in comments (and even in the answer!) reinforce the fact that your question is not clear. You have already received a good answer and already accepted it, so I will not vote to close the question as unclear. But in the future, try to be more careful and explain your need, okay?

Show 5 more comments

1 answer

2


If you’re creating Crawler or if it’s simple, you might want to write it entirely in PHP, but it’s just a suggestion.

Now if you want to send an "input" to another program instead of using system, exec or shell_exec, you should use popen, which will allow you to "chat" with the stream, that is, you can send commands to programs such as telnet (only one example)

It is not necessary fgets or fread, itself popen when closed sends the result to output (STDOUT), if you want to capture the output you can then use ob_start

An example should look like this:

<?php

$input = 'Comando enviado';

$dir = dirname(__FILE__); //Apenas para pegar a pasta relativa

$comando = 'python ' . $dir . '/crawler.py'; //Gera o comando

$handle = popen($comando, 'w'); //inicia o processo

fwrite($handle, $input);

pclose($handle);

And an example of Python (here I use 3.6, but just change the input for raw_input if it’s Python2):

import sys

keyword = input('Keyword: ')

print(keyword)

However if using with other programs or systems maybe you face some problem, then for windows maybe you can use start /B and like-Unix can use 2>&1, thus:

<?php
$dir = dirname(__FILE__);

$comando = $dir . '/crawler.py';
$input = 'Olá mundo!';

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $comando = 'start /B python ' . escapeshellarg($comando);
} else {
    $comando = 'python ' . escapeshellarg($comando) . ' 2>&1';
}

$handle = popen($comando, 'w'); //inicia o processo

fwrite($handle, $input);

pclose($handle);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.