1

I am trying to use the autocompletion of yahoo, I found the link todo it. To do this I am using request in python, I give the right URL and after I do ".get" I get my response. I don´t understand which kind of data is the response. Is it data, array, JSON what is, and how to understand the kind of data in python? How can I extrapolate the single data from this complicated array? I need extract the data for example after the tags: "exchange":"MIL", i need to get MIL "shortname":"MEDIOBANCA", i need Mediobanca How is it possible to do this?

r = requests.get(apiurl)
body=r.text

Response:

 {"explains":[],"count":6,"quotes":[{"exchange":"MIL","shortname":"MEDIOBANCA","quoteType":"EQUITY","symbol":"MB.MI","index":"quotes","score":20129.0,"typeDisp":"Equity","longname":"Mediobanca Banca di Credito Finanziario S.p.A.","isYahooFinance":true},{"exchange":"PNK","shortname":"MEDIOBANCA DI CREDITO FINANZ SP","quoteType":"EQUITY","symbol":"MDIBY","index":"quotes","score":20020.0,"typeDisp":"Equity","longname":"Mediobanca Banca di Credito Finanziario S.p.A.","isYahooFinance":true},{"exchange":"FRA","shortname":"MEDIOBCA  EO 0,50","quoteType":"EQUITY","symbol":"ME9.F","index":"quotes","score":20011.0,"typeDisp":"Equity","longname":"Mediobanca Banca di Credito Finanziario S.p.A.","isYahooFinance":true},{"exchange":"VIE","shortname":"MEDIOBANCA SPA","quoteType":"EQUITY","symbol":"MB.VI","index":"quotes","score":20001.0,"typeDisp":"Equity","longname":"Mediobanca Banca di Credito Finanziario S.p.A.","isYahooFinance":true},{"exchange":"IOB","shortname":"MEDIOBANCA BANCA DI CREDITO FIN","quoteType":"EQUITY","symbol":"0HBF.IL","index":"quotes","score":20001.0,"typeDisp":"Equity","isYahooFinance":true},{"exchange":"STU","shortname":"MEDIOBANCA - BCA CRED.FIN. SPAA","quoteType":"EQUITY","symbol":"ME9.SG","index":"quotes","score":20001.0,"typeDisp":"Equity","isYahooFinance":true}],"news":[],"nav":[],"lists":[],"researchReports":[],"totalTime":19,"timeTakenForQuotes":411,"timeTakenForNews":700,"timeTakenForAlgowatchlist":400,"timeTakenForPredefinedScreener":400,"timeTakenForCrunchbase":0,"timeTakenForNav":400,"timeTakenForResearchReports":0}

Updates:

    list_a = ["mediob"]
list_b = [" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "z",
           "ü", "ä", "ö", "y", "w", "x"] 
list_c = [f"{i} {j}" for i in list_a for j in list_b]
               
for x in list_c:
    apiurl = "https://query1.finance.yahoo.com/v1/finance/search?q="+x+"&quotesCount=6&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&enableNavLinks=true&enableEnhancedTrivialQuery=true" 
    r = requests.get(apiurl)
    data = r.json()
    shortname = data["quotes"][0]["shortname"]
    print(shortname)

it give IndexError: list index out of range

4
  • Please try to improve your explanation. Commented Nov 28, 2020 at 17:29
  • The response needs to be formatted as code. Commented Nov 28, 2020 at 17:36
  • Are you attempting to scrape data off the website? Commented Nov 28, 2020 at 17:46
  • yes, I am trying to get result of the autocompletation Commented Nov 28, 2020 at 17:49

2 Answers 2

0

Well, first off your URLs are not correct. There should be no space here f"{i} {j}" for i in list_a for j in list_b. You just have one URL. It should be [f"{i}{j}" for i in list_a for j in list_b] .Now, the urls generated will be different and we can succesfully scrape the data..for e.g

list_c = [f"{i}{j}" for i in list_a for j in list_b]

for x in list_c:
    apiurl = "https://query1.finance.yahoo.com/v1/finance/search?q="+x+"&quotesCount=6&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&enableNavLinks=true&enableEnhancedTrivialQuery=true"
    r = requests.get(apiurl)
    data = r.json()
    if data["quotes"]:
        shortname = data["quotes"][0]["score"]
        print(shortname)

Output:-

20139.0
20139.0
20011.0
20139.0
20139.0
20139.0

Or for Shortname:- shortname = data["quotes"][0]["shortname"]

MEDIOBANCA
MEDIOBANCA
MEDIOBCA  EO 0,50
MEDIOBANCA
MEDIOBANCA
MEDIOBANCA
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Abhishek and everybody also @gosalia
@DevLeo You have accepted the answer with only one URL. :)
-1
 import requests

list_a = ["mediob"]
list_b = [" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "z",
           "ü", "ä", "ö", "y", "w", "x"] 
list_c = [f"{i} {j}" for i in list_a for j in list_b]
for x in list_c:
    apiurl = "https://query1.finance.yahoo.com/v1/finance/search?q="+x+"&quotesCount=6&quotesQueryId=tss_match_phrase_query&multiQuoteQueryId=multi_quote_single_token_query&enableNavLinks=true&enableEnhancedTrivialQuery=true" 
    r = requests.get(apiurl)
    data = r.json()
    if data['quotes']:
        print(data["quotes"][0]["shortname"])

I took the sample response you provided and created a mock api to simulate what you are doing. The response you get back is basically a json response.

I also see that you have tried the above and are getting an error. The reason is because when some lists are empty so you need to make sure list is not empty before attempting to print print(data["quotes"][0]["shortname"]) hence we have that if statement.

6 Comments

yes, otherwise it give me indentation error
let me try with your updated url, one sec.
@DevLeo can you copy paste my code above and see what output you get? i think this should work
It print the value, but the error stays. Keep saying "IndexError: list index out of range" on print(data["quotes"][0]["shortname"])
where are you executing the code? is it vscode? pycharm? so you are saying you can see the value print now but you also get an error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.