0

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?

Sukhchain Singh
  • 834
  • 1
  • 8
  • 26

2 Answers2

1

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.

zvi
  • 3,677
  • 2
  • 30
  • 48
  • `Traceback (most recent call last): File "/home/myusername/public_html/py/ig.py", line 21, in print(r.text) UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 15318: ordinal not in range(128)` – Sukhchain Singh Mar 18 '19 at 08:00
1

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
Ja8zyjits
  • 1,433
  • 16
  • 31