python - 如何使用 Python 从 HTTP 响应中提取数据?
问题描述
我正在尝试使用 yahoo 的自动补全功能,我找到了要执行此操作的链接。为此,我在 python 中使用请求,我提供了正确的 URL,并且在我执行“.get”之后我得到了我的响应。我不明白响应是哪种数据。是data,array,json是什么,python中数据的种类如何理解?如何从这个复杂的数组中推断出单个数据?例如,我需要在标签之后提取数据:“exchange”:“MIL”,我需要获取 MIL “shortname”:“MEDIOBANCA”,我需要 Mediobanca 怎么可能做到这一点?
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+""esCount=6"esQueryId=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)
它给出了 IndexError: list index out of range
解决方案
好吧,首先你的网址不正确。这里应该没有空间f"{i} {j}" for i in list_a for j in list_b
。您只有一个网址。应该是
[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+""esCount=6"esQueryId=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
推荐阅读
- windows - 是否有不需要管理员权限的 ms Windows tcp/udp/icmp traceroute 实施?
- jsf - p:dataTable filtered, sorted, paginated results get displayed in a single row
- java - Maven 在亚马逊 Linux 上构建失败 Spring Boot 应用程序
- r - R Function to Find Derivative of Every Point in Time Series
- ios - App crashes when run more than once on GMSMAPVIEW
- ubuntu - Google Cloud firewall blocking connections when using port in address
- c# - Generate an image from an HTML code/file in .NET Core 2.x Console Application?
- excel - 使用表单复选框取消隐藏/隐藏excel中的行
- jenkins - Jenkins:基于多个上游作业触发单个下游作业
- php - 不要将 wordpress 基本网址添加到图像路径