循环在烧瓶中的json对象上

时间:2014-12-17 19:45:35

标签: python json api loops flask

@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

这只是吐出了很棒的avatar_url。但现在我想显示用户名以及配置文件的链接。我认为这可以通过一个循环以某种方式完成,但我仍然是Python / Flask的新手,并不确定如何做到这一点。最终输出应如下

Username: "login"
Profile: "url"
img: "avatar_url" 

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

@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放入html img标签并显示它。

1 个答案:

答案 0 :(得分:3)

您可以使用列表解析提取所有网址:

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

但是在Flask中你需要返回一个有效的响应。通常这意味着您返回一个字符串,因此您需要加入这些网址:

return '\n'.join(items)

您还可以返回JSON列表:

from flask import jsonify

# ....

return jsonify(items=items)

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

在Python 2中,您不需要解码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"