-2

I am new to python. Due to I need your help.

I have excel file having some numbers and API of a website where I can download numbers and it returns some info about each number. I 've searched and found that library as requests can do.

import requests 
import pandas as pd 
df=pd.read_excel('numbers.xlsx')
for number in df['num']:
    r = requests.get('https://example.com/datas=%s'%number')
    print (r.text)

However, I don't know how to get data from API response

Output from API response is like that:

age: 0
lat: 70.0000
lon: 70.0000
url: example.com

I wanna only output of age. However, I guess it's not dictionary and not JSON, because I print:

1) print (r['age']) - TypeError: 'Response' object is not subscriptable

Then I did this:

2)

res = r.json()
print(res["age"])

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How can I extract only age value?

Also how is possible to increase speed of the script, because going through 20.000 numbers takes few hours.((

  • 1
    can i know type of api response – venkatadileep Feb 10 '20 at 12:32
  • try printing r and see what object you are getting in return – Andy_101 Feb 10 '20 at 12:33
  • have you tried casting the json as a pandas dataframe: ``df = pd.read_json(r.json())`` ? (see: https://requests.readthedocs.io/en/master/user/quickstart/#response-content ) – heck1 Feb 10 '20 at 12:35
  • response is not in JSON format. You can't get it using `r.json()`. You can manually get first line - `data = r.text.splitlines()[0]` - and skip `"age: "` (4 chars) - `data = data[4:]` or `data = data.replace("age: ", "")`. And then you can convert it to `int()` – furas Feb 10 '20 at 12:39
  • @venkatadileep - XML –  Feb 10 '20 at 12:40
  • @furas - result: Expecting value: line 1 column 1 (char 0) –  Feb 10 '20 at 12:42
  • BTW: it is always good to show real URL. Maybe it has some documentation and maybe it need extra argument to return it as JSON. – furas Feb 10 '20 at 12:45
  • @furas - I can not cuz its private website of company:) Sorry –  Feb 10 '20 at 12:46
  • @LanaBash- try these convert xml to json( https://stackoverflow.com/questions/471946/how-to-convert-xml-to-json-in-python) and run with query it might work . – venkatadileep Feb 10 '20 at 12:54

2 Answers2

0
  1. you might take eventlet into your coding to solve the low speed problem.
  2. as to the error that you are dealing with while use syntax like res['age'], you may try:
import json

res = json.loads(res)
res['age']
xiaojueguan
  • 870
  • 10
  • 19
  • Sorry, but here is : the JSON object must be str, bytes or bytearray, not Response –  Feb 10 '20 at 12:51
0

If you get normal text instead JSON data then you can use string functions for get it.

text = '''age: 0
lat: 70.0000
lon: 70.0000
url: example.com'''

all_lines = text.splitlines()
line = all_lines[0]  # get first line
age = line[5:]  # skip "age: "
#age = int(age) 
print(age)
furas
  • 134,197
  • 12
  • 106
  • 148