I'm trying a simple json.load
import urllib.request
import json
response = urllib.request.urlopen('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.load(response)
and am getting the following error
Traceback (most recent call last):
File "C:\Python33\lib\http\client.py", line 590, in _readall_chunked
chunk_left = self._read_next_chunk_size()
File "C:\Python33\lib\http\client.py", line 562, in _read_next_chunk_size
return int(line, 16)
ValueError: invalid literal for int() with base 16: '\n'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\test.py", line 5, in <module>
data = json.load(url)
File "C:\Python33\lib\json\__init__.py", line 271, in load
return loads(fp.read(),
File "C:\Python33\lib\http\client.py", line 509, in read
return self._readall_chunked()
File "C:\Python33\lib\http\client.py", line 594, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(16384 bytes read)
Based on the error I can't really figure out what might be going on here. Could anyone shed some light on how to get around this?
Edit:
Using the requests module works, but I'd rather use the core Python modules if possible.
import requests
import json
r = requests.get('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.loads(r.text)
This works fine for me
import urllib.request
import json
response = urllib.request.urlopen('https://www.stubhub.com/ticketAPI/restSvc/event/4100000/sort/price/0/')
data = json.loads(response.read().decode('utf-8'))
Related
I am building a test upload api to check dropbox to build final project but I am getting this error when i run python file in cmd:
Traceback (most recent call last):
File "C:\Users\sufiy\Desktop\test.py", line 7, in <module>
dbx.files_upload(file_contents, '/testdropbox.txt', mode=dropbox.files.WriteMode.overwrite)
File "C:\Users\sufiy\AppData\Local\Programs\Python\Python311\Lib\site-packages\dropbox\base.py", line 3210, in files_upload
r = self.request(
^^^^^^^^^^^^^
File "C:\Users\sufiy\AppData\Local\Programs\Python\Python311\Lib\site-packages\dropbox\dropbox_client.py", line 326, in request
res = self.request_json_string_with_retry(host,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sufiy\AppData\Local\Programs\Python\Python311\Lib\site-packages\dropbox\dropbox_client.py", line 476, in request_json_string_with_retry
return self.request_json_string(host,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sufiy\AppData\Local\Programs\Python\Python311\Lib\site-packages\dropbox\dropbox_client.py", line 538, in request_json_string
raise TypeError('expected request_binary as binary type, got %s' %
TypeError: expected request_binary as binary type, got <class 'str'>
here is my code:
import dropbox
dbx = dropbox.Dropbox('my api key')
with open('testdropbox.txt', 'r') as f:
file_contents = f.read()
dbx.files_upload(file_contents, '/testdropbox.txt', mode=dropbox.files.WriteMode('overwrite'))
I tried to build a program that overwrite txt file every minute and I want it to work so I can schedule this by using windows task schedule
This is just simple Python code that worked when I first ran it, but didn't work the next day, even though I didn't change anything:
from pytube import YouTube
link = "https://www.youtube.com/watch?v=6_ardA6TuX0"
video = YouTube(link)
yt = video.streams.get_highest_resolution()
yt.download("Lieder")
I get this Error:
Traceback (most recent call last):
File "C:\Users\edonj\OneDrive\Desktop\Spot\main.py", line 9, in <module>
yt.download("Lieder")
File "C:\Users\edonj\OneDrive\Desktop\Spot\venv\lib\site-packages\pytube\streams.py", line 252, in download
for chunk in request.stream(
File "C:\Users\edonj\OneDrive\Desktop\Spot\venv\lib\site-packages\pytube\request.py", line 185, in stream
chunk = response.read()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 476, in read
s = self._safe_read(self.length)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 628, in _safe_read
raise IncompleteRead(b''.join(s), amt)
http.client.IncompleteRead: IncompleteRead(66612 bytes read, 9370572 more expected)
VideoUnavailable.
Proxy will help you.
I want to parse a json file in python. I don't know the content of the file. I downloaded this file from a website in json format.
As per my knowledge to parse a json file we need this code
import json
sourcefile=open("News_Category_Dataset_v2.json","r")
json_data=json.load(sourcefile)
print (json_data)
But I got this error as describe below. jsonparse.py is my file name which is save in my computer d:/algorithm
D:\python\envs\algorithms\python.exe D:/algorithms/jsonparse.py
Traceback (most recent call last):
File "D:/algorithms/jsonparse.py", line 4, in <module>
json_data=json.load(sourcefile)
File "D:\python\envs\algorithms\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "D:\python\envs\algorithms\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "D:\python\envs\algorithms\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 366)
Process finished with exit code 1
How could I fix the problem?
Your file is not json. but it has lines where each one of them is json.
This snippet should help you
import json
json_list = []
for i in open('test.json'):
json_line = json.loads(i)
json_list.append(json_line)
print(json_list)
I am partially able to work with json saved as file:
#! /usr/bin/python3
import json
from pprint import pprint
json_file='a.json'
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
print(data[10])
But I am trying to achieve the same from data directly from web. I am trying with the accepted answer here:
#! /usr/bin/python3
from urllib.request import urlopen
import json
from pprint import pprint
jsonget=urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")
data = json.load(jsonget)
pprint(data)
which is giving me error:
Traceback (most recent call last):
File "i.py", line 10, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib64/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
What is going wrong here?
Changing the code as par Charlie's reply to:
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee"))
data = json.load(jsonget)
pprint(jsonget)
breaks at json.load:
Traceback (most recent call last):
File "i.py", line 9, in <module>
data = json.load(jsonget)
File "/usr/lib64/python3.5/json/__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
It's actually telling you the answer: you're getting back a byte array, where in Python 3 a string is different because of dealing with unicode. In Python 2.7, it would work. You should be able to fix it by converting your bytes explicitly to a string with
jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")_
When I use first example in Python's nntplib module documentation, there are some errors.
>>> from nntplib import NNTP
>>> s = NNTP('news.gmane.org')
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\nntplib.py", line 354, in group
resp = self.shortcmd('GROUP ' + name)
File "C:\Python27\lib\nntplib.py", line 268, in shortcmd
return self.getresp()
File "C:\Python27\lib\nntplib.py", line 223, in getresp
resp = self.getline()
File "C:\Python27\lib\nntplib.py", line 215, in getline
if not line: raise EOFError
EOFError
Why does this happen?
Try s = NNTP('news.gmane.org', readermode=True)
As mentioned in the docs:
If the optional flag readermode is true, then a mode reader command is
sent before authentication is performed. Reader mode is sometimes
necessary if you are connecting to an NNTP server on the local machine
and intend to call reader-specific commands, such as group. If you get
unexpected NNTPPermanentErrors, you might need to set readermode.