您可以使用列表理解提取所有个网址:
items = [item['avatar_url'] for item in data["items"]]
但在 Flask 中,您确实需要返回有效响应。通常这意味着您返回一个字符串,因此您需要加入这些 URL:
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"