Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 url parse
#1
Quote:Hello,

I'm trying to extract the assets(ADAUSD,BTCUSD,DOGEUSD,DOTUSD,ETHUSD,LTCUSD,SHIBUSD,XMRUSD,XRPUSD,SOLUSD) part of the following url using urllib.parse, but I got this error:

Does anybody can help me please?

Regards!

import urllib.parse

req = requests.get('https://api.kraken.com/0/public/Ticker?pair=ADAUSD,BTCUSD,DOGEUSD,DOTUSD,ETHUSD,LTCUSD,SHIBUSD,XMRUSD,XRPUSD,SOLUSD')
    
parsed = urllib.parse.urlsplit(req)

print("{}?{}".format(parsed.path.split("/")[-1], parsed.query))
Error:
Traceback (most recent call last): File "c:\Users\Orlando\Documents\Chile\Kraken\get_tickerinfoSPT.py", line 19, in <module> parsed = urllib.parse.urlsplit(req.request) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 458, in urlsplit url, scheme, _coerce_result = _coerce_args(url, scheme) return _decode_args(args) + (_encode_result,) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in _decode_args return tuple(x.decode(encoding, errors) if x else '' for x in args) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in <genexpr> return tuple(x.decode(encoding, errors) if x else '' for x in args) AttributeError: 'PreparedRequest' object has no attribute 'decode' PS C:\Users\Orlando\Documents\Chile\Kraken> ^C PS C:\Users\Orlando\Documents\Chile\Kraken> & C:/Users/Orlando/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Orlando/Documents/Chile/Kraken/get_tickerinfoSPT.py Traceback (most recent call last): File "c:\Users\Orlando\Documents\Chile\Kraken\get_tickerinfoSPT.py", line 19, in <module> o = urllib.parse.urlparse(req) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 392, in urlparse File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 128, in _coerce_args return _decode_args(args) + (_encode_result,) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in _decode_args return tuple(x.decode(encoding, errors) if x else '' for x in args) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in <genexpr> return tuple(x.decode(encoding, errors) if x else '' for x in args) AttributeError: 'Response' object has no attribute 'decode' PS C:\Users\Orlando\Documents\Chile\Kraken> & C:/Users/Orlando/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Orlando/Documents/Chile/Kraken/get_tickerinfoSPT.py Traceback (most recent call last): File "c:\Users\Orlando\Documents\Chile\Kraken\get_tickerinfoSPT.py", line 19, in <module> o = urllib.parse.urlparse(req) PS C:\Users\Orlando\Documents\Chile\Kraken> & C:/Users/Orlando/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Orlando/Documents/Chile/Kraken/get_tickerinfoSPT.py Traceback (most recent call last): File "c:\Users\Orlando\Documents\Chile\Kraken\get_tickerinfoSPT.py", line 19, in <module> parsed = urllib.parse.urlsplit(req) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 458, in urlsplit url, scheme, _coerce_result = _coerce_args(url, scheme) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 128, in _coerce_args return _decode_args(args) + (_encode_result,) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in _decode_args return tuple(x.decode(encoding, errors) if x else '' for x in args) File "C:\Users\Orlando\AppData\Local\Programs\Python\Python310\lib\urllib\parse.py", line 112, in <genexpr> return tuple(x.decode(encoding, errors) if x else '' for x in args) AttributeError: 'Response' object has no attribute 'decode'
Reply
#2
use req.text

parsed = urllib.parse.urlsplit(req.text)
Reply
#3
(Mar-04-2022, 04:55 PM)Axel_Erfurt Wrote: use req.text

parsed = urllib.parse.urlsplit(req.text)

Quote:Tnanks Axel,

With .text I can get the entire response, I just need to extract the query part...The assets...
Reply
#4
You need to get the json response back when working with an API
import requests

req = requests.get('https://api.kraken.com/0/public/Ticker?pair=ADAUSD,BTCUSD,DOGEUSD,DOTUSD,ETHUSD,LTCUSD,SHIBUSD,XMRUSD,XRPUSD,SOLUSD')
json_data = req.json()
Now it's a Python dictionary as Requests has build in json decoder.
Can now get data want bye doing ordinary dictionary call or loops.
>>> json_data['result']['ADAUSD']
{'a': ['0.876931', '8000', '8000.000'],
 'b': ['0.876606', '1900', '1900.000'],
 'c': ['0.876637', '56.20427900'],
 'h': ['0.902161', '0.910294'],
 'l': ['0.862667', '0.862667'],
 'o': '0.901642',
 'p': ['0.878015', '0.885684'],
 't': [4687, 5890],
 'v': ['6684590.94553137', '10562996.17641575']}
>>> json_data['result']['ADAUSD']['a']
['0.876931', '8000', '8000.000']

>>> json_data['result']['XXMRZUSD']['h']
['179.00000000', '179.00000000']
Reply
#5
(Mar-04-2022, 06:22 PM)snippsat Wrote: You need to get the json response back when working with an API
import requests

req = requests.get('https://api.kraken.com/0/public/Ticker?pair=ADAUSD,BTCUSD,DOGEUSD,DOTUSD,ETHUSD,LTCUSD,SHIBUSD,XMRUSD,XRPUSD,SOLUSD')
json_data = req.json()
Now it's a Python dictionary as Requests has build in json decoder.
Can now get data want bye doing ordinary dictionary call or loops.
>>> json_data['result']['ADAUSD']
{'a': ['0.876931', '8000', '8000.000'],
 'b': ['0.876606', '1900', '1900.000'],
 'c': ['0.876637', '56.20427900'],
 'h': ['0.902161', '0.910294'],
 'l': ['0.862667', '0.862667'],
 'o': '0.901642',
 'p': ['0.878015', '0.885684'],
 't': [4687, 5890],
 'v': ['6684590.94553137', '10562996.17641575']}
>>> json_data['result']['ADAUSD']['a']
['0.876931', '8000', '8000.000']

>>> json_data['result']['XXMRZUSD']['h']
['179.00000000', '179.00000000']

Quote:Thanks!!

Maybe I haven't explained myself correctly, the only thing I need to extract from the url and store them in some variable is the following data:

ADAUSD, BTCUSD, DOGEUSD....and so on.

Regards!
Reply
#6
(Mar-04-2022, 06:40 PM)ogautier Wrote: the only thing I need to extract from the url and store them in some variable is the following data:

ADAUSD, BTCUSD, DOGEUSD....and so on.
>>> tic = json_data['result'].keys()
>>> tic
dict_keys(['ADAUSD', 'DOTUSD', 'SHIBUSD', 'SOLUSD', 'XDGUSD', 'XETHZUSD', 'XLTCZUSD', 'XXBTZUSD', 'XXMRZUSD', 'XXRPZUSD'])
>>> 
>>> list(tic)
['ADAUSD',
 'DOTUSD',
 'SHIBUSD',
 'SOLUSD',
 'XDGUSD',
 'XETHZUSD',
 'XLTCZUSD',
 'XXBTZUSD',
 'XXMRZUSD',
 'XXRPZUSD']
>>> 
>>> print(' '.join(list(tic)))
ADAUSD DOTUSD SHIBUSD SOLUSD XDGUSD XETHZUSD XLTCZUSD XXBTZUSD XXMRZUSD XXRPZUSD
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020