python code running well locally but not in AWS EC2(ubuntu) - python

I'm trying to parse a website with the requests module on AWS EC2.
import requests
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
print(result.text)
The page is responding as below:
{
"arrangetype":"U",
"list": [
{
"product_no":43,
"display_order":4,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
},
{
"product_no":44,
"display_order":6,
"is_selling":"T",
"product_empty":"F",
"fix_position":null,
"is_auto_sort":false
}
],
"length":2
}
so I did as below thanks to Roadrunner's help(parsing and getting list from response of get request
)
import requests
from json import loads
some_data = {'a':'',
'b':''}
with requests.Session() as s:
result = s.post('http://website.com',data=some_data)
json_dict = loads(result.text)
print([x["product_no"] for x in json_dict["list"]])
It is perfectly working on local PC(windows10), but now i'm working on AWS EC2 to run the code, however, it occurs error.
The name of the file is timesale.py btw.
Traceback (most recent call last):
File "timesale.py", line 657, in <module>
Timesale_Automation()
File "timesale.py", line 397, in Timesale_Automation
status_check()
File "timesale.py", line 345, in status_check
json_dict = loads(now_sale_page2.text)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/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 1 (char 0)
I couldn't figure out why this error occurs.
I looked up overstackflow and there were some articles about decoding json but
result.text.decode('utf-8')
returns, I cannot decode str....
interestingly, I put print(result) between the codes and it is not 'None'
Why this would happen?

Related

Python Json.load JsonDecodeError Expecting value: line 1 column 1 (char 0)

I am trying to read the content of a json file with the function defined below but I keep getting the following traceback:
def clean_email_list_no_slash(json_obj):
dirty_dict = {}
with open(json_obj, 'r') as rdable:
r = json.load(rdable)
dirty_dict.update(r)
for v in dirty_dict.values():
empty_list = []
for email in v['email']:
if not ('/' in email) or not ('\\' in email):
empty_list.append(email)
v['email'] = empty_list
with open(json_obj, 'w') as wtable:
json.dump(dirty_dict, wtable)
print('Done.')
Traceback (most recent call last):
File "c:/Users/EM/Desktop/Scripts/nocws/draft.py", line 306, in <module>
clean_email_list_no_slash(json_obj)
File "c:/Users/EM/Desktop/Scripts/nocws/draft.py", line 292, in clean_email_list_no_slash
r = json.loads(rdable)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I don't get where I am making a mistake.
Here is a sample of the json file:
{
"afghanistan": {
"website": "http://olympic.af/",
"facebook links": [
"https://facebook.com/OlympicOfficial.af"
],
"email": [
"info#olympic.af"
],
"email_links": [
"http://olympic.af/contact-us/"
],
"list_of_urls": [
"http://olympic.af/",
"http://olympic.af/contact-us/"
]
},
"albania": {
...
}
}

How to solve JSONDecodeError while using WHILE loop

while url:
post = session.post(login, data=payload)
r = session.get(url)
parsed = json.loads(r.text)
# Retrieve json product data
if parsed['links']['next'] is not 'null':
url = 'https://testshop.example.com/admin/products' + str(parsed['links']['next'])
time.sleep(2)
for product in parsed['products']:
parsed_result = product['id']
else:
print('stop now!')
break
SO I am using the code above to retrieve and print all the json data in my terminal. Everything is going fine until I retrieve the following error code at the end:
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
Does anybody know what the cause is of this and how I can fix it?
This is my JSON format if that matters:
products: [
{
article_code: "123",
barcode: "456",
brand_id: 2600822,
created_at: "2018-05-31T15:15:34+02:00",
data01: "",
data02: "",
data03: "",
delivery_date_id: null,
has_custom_fields: false,
has_discounts: false,
has_matrix: false,
hits: 0,
hs_code: null,
id: 72660113,
image_id: null,
is_visible: false,
price_excl: 33.0165,
price_incl: 39.95,
price_old_excl: 0,
price_old_incl: 0,
product_set_id: null,
product_type_id: null,
search_context: "123 456 789",
shop_id: 252449,
sku: "789",
supplier_id: 555236,
updated_at: "2018-05-31T15:15:34+02:00",
variants_count: 1,
visibility: "hidden",
weight: 0,
nl: {
content: "",
fulltitle: "Grid Lifter",
slug: "grid-lifter",
title: "Grid Lifter"
}
],
links: {
first: ".json",
last: ".json?page=70",
prev: null,
next: ".json?page=2",
count: 3497,
limit: 50,
pages: 70
}
I am using this to paginate through all the pages.
Traceback:
File "", line 1, in
runfile('loginlightspeedshop.py', wdir='C:/Users/Solaiman/.spyder-py3/SCRIPTS/Lightspeed scripts')
File "sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Solaiman/.spyder-py3/SCRIPTS/Lightspeed scripts/loginshop.py", line 33, in
parsed = json.loads(r.text)
File "C:\Users\Solaiman\Anaconda3\lib\json__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
You are probably getting empty/not json response here:
r = session.get(url)
Try to print r.text before parsing it to detect problem cause. Or use try/except clause:
try:
parsed = r.json()
except ValueError:
print(r.text)
break

Json error : "end is out of bounds"?

I'm having an issue I can't understand. I sending a json data as string via Redis (as a queue) and the receiver is throwing the following error :
[ERROR JSON (in queue)] - {"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature":
"6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"} => end is out of bounds
The code that throws the exception is the following :
try:
item = json.loads(item[1])
except ValueError as e:
sys.stderr.write("[ERROR JSON (in queue)] - {1} => {0}\n".format(str(e), str(item)))
return None
What is really odd, is that if I open a python console and do the following :
>>> import json
>>> s = '{"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature": "6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"}'
>>> print s
I have no issue, the string (copy/pasted in the Python console) yield no errors at all, but my original code is throwing one!
Do you have any idea about what is causing the issue?
You are loading item[1], which is the second character of the string items:
>>> json.loads('"')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: end is out of bounds
You should write:
item = json.loads(item)

Can't access mqlread API

I am trying to access the mqlread API from Freebase but am getting a "Not Found" 404:
api_key = open("freebaseApiKey").read()
mqlread_url = 'https://www.googleapis.com/freebase/v1/mqlread'
mql_query = '[{"mid": null,"name": null, "type": "/location/statistical_region","limit": 100}]'
cursor = ""
topicService_url = 'https://www.googleapis.com/freebase/v1/topic'
params = {
'key': api_key,
'filter': '/location/statistical_region',
'limit': 0
}
for i in xrange(1000):
mql_url = mqlread_url + '?query=' + mql_query + "&cursor=" + cursor
print mql_url
statisticalRegionsResult = json.loads(urllib.urlopen(mql_url).read())
....
Obviously when I run my python file I get:
https://www.googleapis.com/freebase/v1/mqlread?query=[{"mid": null,"name": null, "type": "/location/statistical_region","limit": 100}]&cursor=
Traceback (most recent call last):
File "[Filepath]...FreeBaseDownload.py", line 37, in <module>
statisticalRegionsResult = json.loads(urllib.urlopen(mql_url).read())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
What am I doing wrong with the API? I've read things about mqlread being deprecated, what is the parallel for my quest to get all statistical regions (the mids) in Freebase?
It was deprecated over a year ago. It was finally shut down May 2.
https://groups.google.com/forum/#!topic/freebase-discuss/WEnyO8f7xOQ
The only source for this information now is the Freebase data dump.
https://developers.google.com/freebase/#freebase-rdf-dumps

.json fie handling in python3

So, I've been trying to load a json file using json.load. It is the simplest, most basic code in the world but this is the error that is being thrown followed by the code and then a snippet of the .json file (in case there is a formatting issue):
Error:
Traceback (most recent call last):
File "first.py", line 8, in <module>
data = json.load(input_file)
File /usr/lib/python3.4/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Code:
import json
from pprint import pprint
input_file = open("chennai.recent.02dec2015.jsons",'r', encoding = 'UTF-8')
data = json.load(input_file)
pprint (data)
.json snippet:
{"contributors": null, "truncated": false, "text": "#Chennai Floods Leave Thousands Stranded, More Rain Expected For Coming Days #jobs #500K #: India has deployed... "is_quote_status": false, "in_reply_to_status_id": null, "id": 672203575703965696, "favorite_count": 0

Categories

Resources