When calling for the next tweet of the stream with next(), the following error occurs. When I try calling other functions with the same twitter API object, such as GetFriends(), it works fine. Any help is much appreciated!
Here is the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/twitter/api.py", line 4897, in _ParseAndCheckTwitter
data = json.loads(json_data)
File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.8/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)
And here is the code:
import twitter
class Topic:
def __init__(self, topic):
self.api = self.get_twitter_api()
self.stream = self.api.GetStreamFilter(track=[topic])
self.count_tweets()
def get_twitter_api(self):
with open('/twitter_credentials.json') as f:
return twitter.Api(**eval(f.read()))
def count_tweets(self):
while next(self.stream):
print("tweet")
The oldest trick in the book worked: restarting the PC.
Related
My code:
import requests
x = requests.get('http://pypi.python.org/pypi/urllib3/json')
print(x)
Output:
<Response [403]>
Why am I getting this response even though it works fine in the browser?
I tried to print the json:
import requests
x = requests.get('http://pypi.python.org/pypi/urllib3/json')
print(x.json())
But it gave me an error:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\temp.py", line 5, in <module>
print(x.json())
File "C:\Users\HP\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\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)
It should be https and not http.
This will work.
import requests
x = requests.get('https://pypi.python.org/pypi/urllib3/json')
print(x)
json_str = x.json()
print(json_str)
<Response [200]>
{'info': {'author': 'Andrey Petrov', 'author_email': 'andrey.petrov#shazow.net', 'bugtrack_url': None,....}
Use https://pypi.python.org/pypi/urllib3/json instead, which works fine on my computer.
from requests import get
url = 'https://pypi.python.org/pypi/urllib3/json'
result = get(url).json()
I'm trying to make a special parser for VK, which downloads all music from page of some user, but there's some problem with vk_api, which allows to access audiofiles. I'm trying to call method get() to get list of all tracks, but launch of this program:
session = vk_api.VkApi(token=tkn)
vk = session.get_api()
vk_audio = audio.VkAudio(session)
def get_list_audio():
dct = vk_audio.get(owner_id=owner_id, album_id=None, access_hash=None)
return dct
print(get_list_audio())
shows nothing but an error, connected with json decoder:
Traceback (most recent call last):
File "C:\Users\ann\PycharmProjects\vk-parser\main.py", line 44, in <module>
print(get_list_audio())
File "C:\Users\ann\PycharmProjects\vk-parser\main.py", line 38, in get_list_audio
dct = vk_audio.get(owner_id=owner_id, album_id=None, access_hash=None)
File "D:\PycharmProjects\vk-parser\lib\site-packages\vk_api\audio.py", line 158, in get
return list(self.get_iter(owner_id, album_id, access_hash))
File "D:\PycharmProjects\vk-parser\lib\site-packages\vk_api\audio.py", line 107, in get_iter
response = self._vk.http.post(
File "D:\PycharmProjects\vk-parser\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Python 3.9\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Python 3.9\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python 3.9\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)
Please, give some hints, what can I do?
vk_api/audio.py makes just POST request to m.vk.com/audio:
and the error occures when it tries to convert the response to json (line 119). If we run these lines manually (or you can use debugger anytime to see what happens), we see 302 http error with empty response. json() can't get json from this empty response, so you see the exception. You have to choose another library (to work with vk api) or to write these actions yourself. Or to fix this lib code :)
Probably, this vk_api library does not support the changes that have occurred in vk.com. You can take this code from the library and make your own method for getting audio based on it.
After generating the access_token (which works when I use it on TD Ameritrade's API website) I'm trying to get option chains for a stock. I can get it to work on TD Ameritrade's API website, and I get an 'OKAY' response when I run my code, but no JSON data attached, any idea why? My relevant code is below.
content = requests.get(url = https://api.tdameritrade.com/v1/marketdata/chains, params = params_dictionary, headers = access_token)
print(content)
print(repr(content.text))
data = content.json()
print(data)
but for my output I get
<Response [200]>
''
Traceback (most recent call last):
File "C:\Users\USER\Documents\GitHub\pythonfiles\TD Ameritrade API tests.py", line 98, in <module>
data = content.json()
File "C:\Users\USER\Anaconda3\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\USER\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\USER\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\USER\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
It was a mistake on my part unshown in my question. I was accidentally using 'https://api.tdameritrade.com/v1/marketdata/https://api.tdameritrade.com/v1/marketdata/chains' for my URL. I'm unsure how this didn't break things, but this was my issue.
The error "expecting value" is occurring when I'm trying to read a json file which I have created myself. The weird thing is that this used to work. I was working on another project in between and did some updates to conda and now I'm getting the error. I've searched and found similar questions but none that have been applicable in my case.
This is what I'm doing right now:
def show_json(filepath):
with open("/Users/human/Doc/PYTHON/bn/bread/"+filepath) as f:
k = json.load(f)
return k
state_d = show_json("states_json.json")
Here is the error traceback:
File "/Users/human/PYTHON/SPYDER/bn/bread/main_.py", line 55, in <module>
state_d=show_json("states_json.json")
File "/Users/human/PYTHON/SPYDER/bn/bread/main_.py", line 48, in show_json
k = json.load(f)
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/human/opt/anaconda3/envs/futures/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
And this is what the json file looks like:
{"_state_one_":None,"_state_two_":None,"_state_three_":None,"_state_four_":None}
Could something have happened while updating some libraries and packages or am I just doing something wrong code-wise?
Your JSON file is simply not compliant to the JSON specifications. Shortly put, null should be used instead of None
I've written a script in python to get response from a webpage. The data in that webpage are in json format. However, when I try like below I get an error. Can somebody give me any workaround as to how I can get a valid response?
Here is my failure attempt:
import requests
import json
URL = "https://www.sandicormls.com/agent/AgentSearch?officeSortOption=name&_=1516998894917&_keywordsAll=&officeLocation=&sortOption=rndsrt&_keywordsAgentName=&page=&typeIn=Realtor%2CBroker%2COwner%2COffice+Manager%2CAppraiser&searchMode=agent&officeName="
res = requests.get(URL,headers={'User-Agent':'Mozilla/5.0'})
print(res.json())
This is the traceback:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python35-32\new_line_one.py", line 34, in <module>
print(res.json())
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\AppData\Local\Programs\Python\Python35-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 1 (char 0)
I just gave it a try and confirm that your code is fine as it works on my environment. Though, I run it on a Linux (Ubuntu 16.04) virtual machine.
You could check what data you are getting back
print(res.headers[‘content-type’])
or examine the content such as
print(res.text)