2014-12-17 4 views
0
@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에 대해 매우 익숙하고 이것이 어떻게 행해질 지 확실히 알지 못한다.플라스크의 json 객체를 반복합니다.

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> 

내가이 HTML의 img 태그에 avatar_url를 넣고 그것을 표시 할 계획 검색 폼에 새 값을 입력 다음과 같이 최종 출력해야한다.

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

을하지만 플라스크에 유효한 응답을 반환해야합니까 :

+0

은 아마이 파이썬 3? 'getparam()'은 기본값으로 사용되며'get_charset()'을 사용할 수 있습니다. –

+0

* 예상되는 결과 *는 무엇입니까? URL 목록? 그걸 어떻게 요청자에게 돌려 주어야합니까? 긴 문자열 하나? –

+0

python 2.7 – KittenCornBall

답변

3

당신은 지능형리스트로 모든 URL을 추출 할 수 있습니다. 보통 그것은 당신이 그 URL을 가입해야 거라고 있도록, 문자열을 반환 의미

return '\n'.join(items) 
당신은 또한 JSON 목록을 반환 할 수

:이 키 'items' 가리키는 JSON 개체를 반환

from flask import jsonify 

# .... 

return jsonify(items=items) 

을 목록에; jsonify() function은 올바른 application/json 헤더가 설정된 올바른 Response() 개체를 만듭니다.

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" 
+0

이것은 좋지만 루프를 통해 사용자 이름/avatar_url/url을 얻고 싶습니다. – KittenCornBall

+0

@ user3079589 : Flask는 * 적절한 응답 *을 반환해야합니다. 목록을 반환 할 수는 없습니다. 기대하시는 답변은 무엇입니까? JSON 데이터, 문자열, 다른 뭔가? 제발 ** ** 명시 적이어야합니다. 해당 정보를 포함하도록 질문을 업데이트 할 수 있습니다. –

+0

im은 아바타와 프로필 URL을 가진 문자열을 기대합니다 – KittenCornBall

관련 문제