0

I'm trying to get only the keys' values that are exact to my 'title' query but the results seem to be anything that contains at least a part of the query which is very inexact. If I type "noragami", for example, I only want to get the results that have that word in them but that's not happening.

For example, if I search that, I get:

{
    "Title": "Noragami OVA",
    "Episodes": 2,
    "Image": "https://cdn.myanimelist.net/images/anime/7/77177.jpg?s=189ec6d865ed53e2e5195ba05a632fff"
}
{
    "Title": "Noragami Aragoto OVA",
    "Episodes": 2,
    "Image": "https://cdn.myanimelist.net/images/anime/11/77510.jpg?s=9e9261ac9140accd6844392db5d9a952"
}
{
    "Title": "Noraneko",
    "Episodes": 1,
    "Image": "https://cdn.myanimelist.net/images/anime/2/88357.jpg?s=4df00538a268a9927f352d2b5718d934"
}

The last one shouldn't be there so how can I fix this?

This is the code for it:

search_params = {
            'animes' : 'title',
            'q' : request.POST['search']
            
            }

        
r = requests.get(animes_url, params=search_params)
results = r.json()
        
results = results['results']
output = []

for result in results:
    animes_data = {

                'Title' : result["title"],
                'Episodes' : result["episodes"],
                'Image' : result["image_url"]
            }
    output.append(animes_data)


[print(json.dumps(item, indent=4)) for item in output]
dazai
  • 766
  • 4
  • 25
  • 1
    can't you force the api to perform a stricter search? It looks to me like it performs close match search instead of exact match. – DevLounge May 07 '21 at 21:03
  • Your final line list-comprehension-with-print-side-effect is a bit of an anti-pattern. This SO answer says why far better than I can https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects#5753614 – DisappointedByUnaccountableMod May 07 '21 at 21:03
  • @DevLounge The documentation doesn't seem to say so but I guess I will just limit the amount of results I get – dazai May 07 '21 at 21:06
  • @barny Oh, I see, I'll check it out, thank you. – dazai May 07 '21 at 21:08
  • What exactly does `'q' : request.POST['search']` do? What’s the value for the ‘q’ key? – DisappointedByUnaccountableMod May 07 '21 at 21:11
  • @barny q is the query I need to get the result from "https://api.jikan.moe/v3/search/anime?q={}&limit=6&page=1" and it's basically the 'title' – dazai May 07 '21 at 21:20
  • It’s not clear from your comment: is ‘q’ ‘{}’ or something else? Where does `limit=6&page=1` come from? FYI you can’t put those in the value for `q` because they’ll be url encoded and therefore _won’t_ be parameters to the search. Have you checked that the resulting URL used by the `requests.get` call after adding your params is actually the query you hope/expect/want? – DisappointedByUnaccountableMod May 07 '21 at 21:28
  • @barny Yes, 'q' is '{}' and I added the limit so I can only get a few elements since I cannot get the exact element I want and the page is so it won't keep looking in other pages. I understand. The query does work, it just returns more results than I would like. I suppose I'll stick to the 'limit' add-on for now. Thank you anyway. – dazai May 07 '21 at 21:56

1 Answers1

1

Try this :

search_str = 'noragami'

for result in results:
    if search_str in result["title"]: 
        animes_data = {
            'Title' : result["title"],
            'Episodes' : result["episodes"],
            'Image' : result["image_url"]
        }
        output.append(animes_data)
Charles Dupont
  • 995
  • 3
  • 9