2
import json
import requests
import sys

exampleURL = 'https://apps.runescape.com/runemetrics/quests?user=Marebelle'

questName = 'All Fired Up'
response = requests.get(exampleURL)

if response.status_code==200:
    questData = response.content.decode('utf-8')

how can I search for questName and have it print this only {"title":"All Fired Up","status":"COMPLETED","difficulty":1,"members":true,"questPoints":1,"userEligible":true}

sorry for bad formatting, any help is appreciated, thanks!

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 2
    You import the `json` module but you don't use it. Parse `questData` with `json.loads(questData)` and find the object you're looking for in the dictionary. – Blender Mar 17 '18 at 00:38
  • 1
    How to format your post: Use four spaces to indent and then use ` (thing next to !) to format individual parts of code. – Xantium Mar 17 '18 at 00:39
  • Please include an example of `questData` (a valid one)l. – DYZ Mar 17 '18 at 00:55
  • Say if questData is a long list of strings like this: {"title":"All Fired Up","status":"COMPLETED","difficulty":1,"members":true,"questPoints":1,"userEligible":true}, how would we be able to search All Fired Up in this long list of strings, then print all the information between the { }? Does that clarify what I am asking? – Sir Johnson Mar 17 '18 at 01:06
  • If you know "All Fired Up" *is* one of the the values for `"title"`, then we can search for it using `if questData["title"] == "All Fired Up"`. maybe looking at the behavior of the json keys may help you better understand the json response, try for k in data.keys(): print data[k] – chickity china chinese chicken Mar 17 '18 at 01:16
  • Anton VbR's code works, but it prints in a random order, would you be able to explain why that would be happening? it prints a random order each time the code is ran. – Sir Johnson Mar 17 '18 at 01:25
  • @SirJohnson, the order is not random, python prints the dictionary keys and values based on hashing – chickity china chinese chicken Mar 17 '18 at 01:28
  • @davedwards is there a way to print the dictionary keys and values based on hashing in a specific order? – Sir Johnson Mar 17 '18 at 01:30
  • @SirJohnson perhaps look into [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict), and this answer [Python dictionary, how to keep keys/values in same order as declared?](https://stackoverflow.com/questions/1867861/python-dictionary-how-to-keep-keys-values-in-same-order-as-declared) – chickity china chinese chicken Mar 17 '18 at 01:32
  • That would definitely help, thank you for your time – Sir Johnson Mar 17 '18 at 01:36

3 Answers3

0

Implement Blender's suggestion from the comments, add these lines below the code you have already:

data = json.loads(questData)

for quest in data['quests']:
    if quest['title'] == questName:
        print quest

will get:

{u'status': u'COMPLETED', u'title': u'All Fired Up', u'userEligible': True, u'difficulty': 1, u'members': True, u'questPoints': 1}
  • I am confused on what quests is defined as in your for statement, please explain – Sir Johnson Mar 17 '18 at 01:08
  • The available keys in `response` are `[u'loggedIn', u'quests']`, you can see this if you inspect the response from `print data.keys()`, and the information you want is within the values gotten from the `quests` key, so we need to parse the values in `data[quests]`. – chickity china chinese chicken Mar 17 '18 at 01:11
0

Here is my take:

import requests

exampleURL = 'https://apps.runescape.com/runemetrics/quests?user=Marebelle'

questName = 'All Fired Up'
response = requests.get(exampleURL)

if response.status_code==200:
    questData = next((i for i in response.json()['quests'] if i['title']== questName),None)
    print(questData)

Returns:

{'difficulty': 1,
 'members': True,
 'questPoints': 1,
 'status': 'COMPLETED',
 'title': 'All Fired Up',
 'userEligible': True}

Update:

import requests
from collections import OrderedDict

exampleURL = 'https://apps.runescape.com/runemetrics/quests?user=Marebelle'

questName = 'All Fired Up'
keys = ['title','status','difficulty','members','questPoints','userEligible']

response = requests.get(exampleURL)

if response.status_code==200:
    questData = next((i for i in response.json()['quests'] if i['title']== questName),None)
    d = OrderedDict((key,questData.get(key,'')) for key in keys)
    print(d)
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • Ok, it prints all the information I want but its not in the correct order you listed it as, it prints it in a random order each time I run the code. First run: {'members': True, 'userEligible': True, 'status': 'COMPLETED', 'title': 'All Fired Up', 'questPoints': 1, 'difficulty': 1} Second Run: {'members': True, 'title': 'All Fired Up', 'difficulty': 1, 'status': 'COMPLETED', 'questPoints': 1, 'userEligible': True} – Sir Johnson Mar 17 '18 at 01:19
  • so why would it be in a random order? – Sir Johnson Mar 17 '18 at 01:20
  • @SirJohnson Because python dictionaries are hashed and by design are unordered, see here [Why is the order in dictionaries and sets arbitrary?](https://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary) – chickity china chinese chicken Mar 17 '18 at 01:25
  • Is there a way to not make it unordered? @davedw – Sir Johnson Mar 17 '18 at 01:28
  • @SirJohnson You mean you want them in order? Dictionaries are typically not meant to have order but accessed through keys. – Anton vBR Mar 17 '18 at 01:42
  • I just made a dictionary with the orderdereddict, now I need to store the information from questData into specific locations in a dictionary, any ideas? – Sir Johnson Mar 17 '18 at 01:55
  • @SirJohnson I updated with an example code you can use. – Anton vBR Mar 17 '18 at 02:05
  • @SirJohnson I updated with a shorter and more readable version. – Anton vBR Mar 17 '18 at 02:09
0
import json
import requests
import sys

exampleURL = 'https://apps.runescape.com/runemetrics/quests?user=Marebelle'

questName = 'All Fired Up'
response = requests.get(exampleURL)

if response.status_code==200:
    res = response.json()
    for y in res['quests']:
        if(y["title"] == questName):
            print(y)
Amr M. Kayid
  • 113
  • 2
  • 8