@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标签并显示出来。在
您可以通过列表理解来提取所有URL:
但在Flask中,您需要返回一个有效的响应。通常这意味着您返回一个字符串,因此您需要连接这些URL:
^{pr2}$您还可以返回一个JSON列表:
这将返回一个JSON对象,其中键} function 为您创建了一个正确的
'items'
指向列表;^{Response()
对象,并为您设置了正确的application/json
头。在在python2中,不需要解码JSON响应;GitHub坚持JSON RFC并返回UTF编码的数据:
演示:
如果您想将更多信息提取到字符串中,我将使用string formatting:
每个
{}
占位符从使用i
关键字参数传递的项中提取不同的键。在相同搜索的演示:
相关问题 更多 >
编程相关推荐