Reputation: 844
I am trying to use Python Requests via PHP for scraping a page
index.php:
<?php
$url = $_GET['url'];
$output = shell_exec("python /home/myusername/public_html/py/fetch.py $url");
echo $output;
and here is my fetch.py:
#! /usr/bin/python
import requests
import sys
url = sys.argv[1]
headers = {'cache-control': 'no-cache'}
try:
r = requests.request("GET", url, headers=headers)
#print r.status_code
#sys.exit(1)
except requests.exceptions.RequestException as e:
print e
sys.exit(1)
if not r.text:
print("Response Empty")
else:
print(r.text)
I tried checking status code, it's 200. I tried checking if response is empty, it's not. But r.text is not printing at all. What i am doing wrong here?
Upvotes: 0
Views: 2599
Reputation: 4706
Trying this way:
shell_exec("python /home/myusername/public_html/py/fetch.py $url 2>&1");
you can see the errors in the python script you have.
Upvotes: 1
Reputation: 1502
According to the official documentation there are some catches included in shell_exec
You may have to update your code a bit. There are chances that you may have error in your code, which are basically redirected to stderr
and hence not captured in the stdout
. Further read this
So back to your question. Edit your code
<?php
$url = $_GET['url'];
$output = shell_exec("python /home/myusername/public_html/py/fetch.py $url 2>&1");
echo $output;
?>
Update Python code to:
#! /usr/bin/python
import requests
import sys
url = sys.argv[1]
headers = {'cache-control': 'no-cache'}
try:
r = requests.request("GET", url, headers=headers)
#print r.status_code
#sys.exit(1)
except requests.exceptions.RequestException as e:
print e
sys.exit(1)
if not r.text:
print("Response Empty")
else:
print(r.text.encode('utf-8')) # changes
Upvotes: 1