1

I am trying to send array of URI's from my php to my python scraper. the array contains links to scrape.

I got example from here, and it worked fine with sending array of integer, but when I try to fill the array using URI's, error showed up.

php snippet:

$array = ["https://www.google.com/","https://www.google.com/","https://www.google.com/"];
//$array = [1,2,3]; // This is worked fine
$resultScript= system('python C:\xampp\htdocs\selenium\dummy.py '   .escapeshellarg(json_encode($array)));
$resultData = json_decode($resultScript, true);
var_dump($resultData);

python :

import sys
import json
def jsontoarray(json_data):
    data = json.loads(json_data)
    print(json.dumps(data))
jsontoarray(sys.argv[1])
print(data)

result from my IDE

Traceback (most recent call last):
  File "C:\xampp\htdocs\selenium\dummy.py", line 8, in <module>
    jsontoarray(sys.argv[1])
  File "C:\xampp\htdocs\selenium\dummy.py", line 6, in jsontoarray
    data = json.loads(json_data)
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\PC2\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 3 (char 2)
NULL

Process finished with exit code 0
Fredd
  • 65
  • 1
  • 6

1 Answers1

0

As said here, reason of getting error like:

json.decoder.JSONDecodeError: Expecting value: line 1 column 3 (char 2)

can be:

  • non-JSON conforming quoting
  • XML/HTML output (that is, a string starting with <), or
  • incompatible character encoding

So based what you have I would bet that there is encoding problem.

  • Take a look on your PHP file and make sure it's UTF-8 encoded.
  • Check Python script file encoding and it would be good if you add at the beginning of the file line below to enforce proper encoding.

    # -*- coding: utf-8 -*-

so your python file will look like:

# -*- coding: utf-8 -*-
import sys
import json
def jsontoarray(json_data):
    data = json.loads(json_data)
    print(json.dumps(data))
jsontoarray(sys.argv[1])

additionally for debuging you could print type of sys.argv[1]:

print(type(sys.argv[1]))

you should get string or bytes (for UTF-8) string. If you got something else you could convert it into string like:

str(sys.argv[1])
Tomasz
  • 4,847
  • 2
  • 32
  • 41