如何使用Python从HTTP响应中提取数据?

2024-05-14 21:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用雅虎的自动完成功能,我找到了它的链接。 为此,我在python中使用request,我给出了正确的URL,在我使用“.get”之后,我得到了我的响应。我不明白哪种数据是回应。是数据、数组、JSON是什么,以及如何理解python中的数据类型? 如何从这个复杂的数组中推断出单个数据?我需要提取数据,例如在标记之后: “交换”:“MIL”,我需要得到MIL “短名”:“麦迪奥班卡”,我需要麦迪奥班卡 这怎么可能呢

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

答复:

 {"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}

更新:

    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)

它给出了索引器:列表索引超出范围


Tags: 数据trueindexexchangesymbollistquotesscore
2条回答

首先,你的URL是不正确的。这里应该没有空间f"{i} {j}" for i in list_a for j in list_b。您只有一个URL。应该是 [f"{i}{j}" for i in list_a for j in list_b]。现在,生成的URL将不同,我们可以成功地抓取数据

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)

输出:-

20139.0
20139.0
20011.0
20139.0
20139.0
20139.0

或简称:- shortname = data["quotes"][0]["shortname"]

MEDIOBANCA
MEDIOBANCA
MEDIOBCA  EO 0,50
MEDIOBANCA
MEDIOBANCA
MEDIOBANCA
 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"])

我获取了您提供的示例响应,并创建了一个模拟api来模拟您正在做的事情。返回的响应基本上是json响应

我还看到,您尝试了上述方法,但出现了错误。原因是,当某些列表为空时,您需要确保在尝试打印print(data["quotes"][0]["shortname"])之前列表不是空的,因此我们有if语句

相关问题 更多 >

    热门问题