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?