在flas中循环json对象

2024-10-05 13:21:21 发布

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

@app.route('/parseJSON',methods=['GET','POST'])
def parseJSON():
    input_name = request.form["search"]
    url = "https://api.github.com/search/users?q={0}".format(input_name)
    loadurl = urllib.urlopen(url)
    data = json.loads(loadurl.read().decode(loadurl.info().getparam('charset') or 'utf-8'))
    item=data["items"][0]["avatar_url"]
    return item

这只是吐出阿凡达网址,这是伟大的。但现在我想显示用户名和配置文件的链接。我认为这可以用一个循环来完成,但我对Python/Flask还是个新手,不太确定如何实现这一点。最终输出如下

^{pr2}$

这将更新所有人在搜索表单中输入新值的内容

@app.route('/search',methods=['GET','POST'])
def search():
    return render_template("search.html")

<form action="/parseJSON" method="POST">
    <input type="text" name="search">
    <input type="submit">
</form>

我计划将avatar_url放入htmlimg标签并显示出来。在


Tags: nameformappurlinputsearchdataget
1条回答
网友
1楼 · 发布于 2024-10-05 13:21:21

您可以通过列表理解来提取所有URL:

items = [item['avatar_url'] for item in data["items"]]

但在Flask中,您需要返回一个有效的响应。通常这意味着您返回一个字符串,因此您需要连接这些URL:

^{pr2}$

您还可以返回一个JSON列表:

from flask import jsonify

# ....

return jsonify(items=items)

这将返回一个JSON对象,其中键'items'指向列表;^{} function为您创建了一个正确的Response()对象,并为您设置了正确的application/json头。在

在python2中,不需要解码JSON响应;GitHub坚持JSON RFC并返回UTF编码的数据:

@app.route('/parseJSON',methods=['GET','POST'])
def parseJSON():
    input_name = request.form["search"]
    url = "https://api.github.com/search/users?q={0}".format(input_name)
    loadurl = urllib.urlopen(url)
    data = json.load(loadurl)
    items = [item['avatar_url'] for item in data["items"]]
    return '\n'.join(items)

演示:

>>> import urllib
>>> import json
>>> url = "https://api.github.com/search/users?q=martijn"
>>> loadurl = urllib.urlopen(url)
>>> data = json.load(loadurl)
>>> items = [item['avatar_url'] for item in data["items"]]
>>> print '\n'.join(items)
https://avatars.githubusercontent.com/u/107915?v=3
https://avatars.githubusercontent.com/u/623509?v=3
https://avatars.githubusercontent.com/u/431452?v=3
https://avatars.githubusercontent.com/u/46626?v=3
https://avatars.githubusercontent.com/u/46775?v=3
https://avatars.githubusercontent.com/u/180840?v=3
https://avatars.githubusercontent.com/u/245275?v=3
https://avatars.githubusercontent.com/u/670951?v=3
https://avatars.githubusercontent.com/u/121401?v=3
https://avatars.githubusercontent.com/u/506862?v=3
https://avatars.githubusercontent.com/u/627350?v=3
https://avatars.githubusercontent.com/u/2605679?v=3
https://avatars.githubusercontent.com/u/4040870?v=3
https://avatars.githubusercontent.com/u/120452?v=3
https://avatars.githubusercontent.com/u/167455?v=3
https://avatars.githubusercontent.com/u/965129?v=3
https://avatars.githubusercontent.com/u/515239?v=3
https://avatars.githubusercontent.com/u/197477?v=3
https://avatars.githubusercontent.com/u/178230?v=3
https://avatars.githubusercontent.com/u/490579?v=3
https://avatars.githubusercontent.com/u/1426964?v=3
https://avatars.githubusercontent.com/u/327472?v=3
https://avatars.githubusercontent.com/u/193881?v=3
https://avatars.githubusercontent.com/u/907436?v=3
https://avatars.githubusercontent.com/u/6215449?v=3
https://avatars.githubusercontent.com/u/580421?v=3
https://avatars.githubusercontent.com/u/3951973?v=3
https://avatars.githubusercontent.com/u/426811?v=3
https://avatars.githubusercontent.com/u/1290310?v=3
https://avatars.githubusercontent.com/u/1652861?v=3

如果您想将更多信息提取到字符串中,我将使用string formatting

template = '''\
Username: "{i[login]}"
Profile: "{i[url]}"
img: "{i[avatar_url]}"
'''
items = [template.format(i=item) for item in data["items"]]
return '\n'.join(items)

每个{}占位符从使用i关键字参数传递的项中提取不同的键。在

相同搜索的演示:

>>> template = '''\
... Username: "{i[login]}"
... Profile: "{i[url]}"
... img: "{i[avatar_url]}"
... '''
>>> items = [template.format(i=item) for item in data["items"]]
>>> print '\n'.join(items)
Username: "martijn"
Profile: "https://api.github.com/users/martijn"
img: "https://avatars.githubusercontent.com/u/107915?v=3"

Username: "martijnvermaat"
Profile: "https://api.github.com/users/martijnvermaat"
img: "https://avatars.githubusercontent.com/u/623509?v=3"

Username: "mvdkleijn"
Profile: "https://api.github.com/users/mvdkleijn"
img: "https://avatars.githubusercontent.com/u/431452?v=3"

Username: "dashorst"
Profile: "https://api.github.com/users/dashorst"
img: "https://avatars.githubusercontent.com/u/46626?v=3"

Username: "mjpieters"
Profile: "https://api.github.com/users/mjpieters"
img: "https://avatars.githubusercontent.com/u/46775?v=3"

Username: "karianna"
Profile: "https://api.github.com/users/karianna"
img: "https://avatars.githubusercontent.com/u/180840?v=3"

Username: "Mpdreamz"
Profile: "https://api.github.com/users/Mpdreamz"
img: "https://avatars.githubusercontent.com/u/245275?v=3"

Username: "Swaagie"
Profile: "https://api.github.com/users/Swaagie"
img: "https://avatars.githubusercontent.com/u/670951?v=3"

Username: "maerteijn"
Profile: "https://api.github.com/users/maerteijn"
img: "https://avatars.githubusercontent.com/u/121401?v=3"

Username: "MartijnB"
Profile: "https://api.github.com/users/MartijnB"
img: "https://avatars.githubusercontent.com/u/506862?v=3"

Username: "MartijnR"
Profile: "https://api.github.com/users/MartijnR"
img: "https://avatars.githubusercontent.com/u/627350?v=3"

Username: "Speedy1985"
Profile: "https://api.github.com/users/Speedy1985"
img: "https://avatars.githubusercontent.com/u/2605679?v=3"

Username: "Azeirah"
Profile: "https://api.github.com/users/Azeirah"
img: "https://avatars.githubusercontent.com/u/4040870?v=3"

Username: "mvexel"
Profile: "https://api.github.com/users/mvexel"
img: "https://avatars.githubusercontent.com/u/120452?v=3"

Username: "martijnboland"
Profile: "https://api.github.com/users/martijnboland"
img: "https://avatars.githubusercontent.com/u/167455?v=3"

Username: "Martijnc"
Profile: "https://api.github.com/users/Martijnc"
img: "https://avatars.githubusercontent.com/u/965129?v=3"

Username: "Pixelstudio"
Profile: "https://api.github.com/users/Pixelstudio"
img: "https://avatars.githubusercontent.com/u/515239?v=3"

Username: "mvmaasakkers"
Profile: "https://api.github.com/users/mvmaasakkers"
img: "https://avatars.githubusercontent.com/u/197477?v=3"

Username: "martijndeh"
Profile: "https://api.github.com/users/martijndeh"
img: "https://avatars.githubusercontent.com/u/178230?v=3"

Username: "Zegnat"
Profile: "https://api.github.com/users/Zegnat"
img: "https://avatars.githubusercontent.com/u/490579?v=3"

Username: "mgussekloo"
Profile: "https://api.github.com/users/mgussekloo"
img: "https://avatars.githubusercontent.com/u/1426964?v=3"

Username: "faassen"
Profile: "https://api.github.com/users/faassen"
img: "https://avatars.githubusercontent.com/u/327472?v=3"

Username: "martijnthe"
Profile: "https://api.github.com/users/martijnthe"
img: "https://avatars.githubusercontent.com/u/193881?v=3"

Username: "MartijnKaijser"
Profile: "https://api.github.com/users/MartijnKaijser"
img: "https://avatars.githubusercontent.com/u/907436?v=3"

Username: "ByteHazard"
Profile: "https://api.github.com/users/ByteHazard"
img: "https://avatars.githubusercontent.com/u/6215449?v=3"

Username: "martijnvg"
Profile: "https://api.github.com/users/martijnvg"
img: "https://avatars.githubusercontent.com/u/580421?v=3"

Username: "MartijnWoudstra"
Profile: "https://api.github.com/users/MartijnWoudstra"
img: "https://avatars.githubusercontent.com/u/3951973?v=3"

Username: "MartijnDwars"
Profile: "https://api.github.com/users/MartijnDwars"
img: "https://avatars.githubusercontent.com/u/426811?v=3"

Username: "cornips"
Profile: "https://api.github.com/users/cornips"
img: "https://avatars.githubusercontent.com/u/1290310?v=3"

Username: "martijn94"
Profile: "https://api.github.com/users/martijn94"
img: "https://avatars.githubusercontent.com/u/1652861?v=3"

相关问题 更多 >

    热门问题