2017-12-28 22 views
0

Ich habe Probleme mit der NYTimes Article Search API. Ich benutze das Python-Paket "NYTimesArctile" und ich versuche, mit einem einfachen Beispiel zu arbeiten, nur damit ich vertraut/vertraut mit der Weitergabe von Daten an Webseiten von den NYTimes werden kann.NYTimes Artikelsuche API - UndefinedError: 'Unicode-Objekt' hat kein Attribut 'Repsonse'

hier ist mein app.py

from flask import Flask 
from flask_mysqldb import MySQL 
from nytimesarticle import articleAPI 

api = articleAPI("*****") 

app = Flask(__name__) 
app.config['MYSQL_HOST'] = 'localhost' 
app.config['MYSQL_USER'] = 'root' 
app.config['MYSQL_PASSWORD'] = '*****' 
app.config['MYSQL_DB'] = '*****' 
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' 
app.config['SECRET_KEY'] = '*****' 

mysql = MySQL(app) 

from views import * 

if __name__=='__main__': 
    app.run(debug=True) 

Hier ist, wo meiner Meinung nach für eine "funktionsfähige Seite":

@app.route('/featured', methods=['GET']) 
def featured(): 
    results = api.search(q = 'Obama') 
    return render_template('featured.html', results=results) 

Hier meine HTML-Seite ist, featured.html:

{% extends 'base/base.html' %} 

{% block body %} 

<div class="container"> 

<table class="table table-sm table-light"> 
    <thead> 
    <tr> 
     <th scope="col">title</th> 
     <th scope="col">URL</th> 
     <th></th> 
    </tr> 
    </thead> 
    <tbody> 
    {% for result in results %} 
    <tr> 
     <td>{{result.title() }}</td> 
     <td>{{ result.title() }}</td> 
     <td></td> 
    </tr> 
    {% endfor %} 
    </tbody> 
</table> 

</div> 


{% endblock %} 

Ich habe results.title() im Jinja-Format platziert, weil es das einzige ist, was funktioniert. Wenn ich results.title() zurückgebe, bekomme ich die ersten 3 Objekte aus den JSON-Daten (Status, Response, Copyright). Was ich will, ist der Titel des Artikels und der web_url.

Ich weiß, dass die Info, die ich will, in results.response.docs ist, aber wenn ich das versuche, bekomme ich den obigen Fehler.

Hier ist ein bisschen der JSON von NYTimes (ein Artikel). Jede Hilfe ist willkommen:

{ 
    "status": "OK", 
    "copyright": "Copyright (c) 2017 The New York Times Company. All Rights Reserved.", 
    "response": { 
    "docs": [ 
     { 
     "web_url": "https://www.nytimes.com/2017/12/01/books/review/peter-baker-obama-the-call-of-history.html", 
     "snippet": "Peter Baker’s “Obama: The Call of History” is a tribute to a man and an office.", 
     "print_page": "53", 
     "blog": {}, 
     "source": "The New York Times", 
     "multimedia": [ 
      { 
      "rank": 0, 
      "subtype": "xlarge", 
      "caption": null, 
      "credit": null, 
      "type": "image", 
      "url": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-articleLarge.jpg", 
      "height": 397, 
      "width": 600, 
      "legacy": { 
       "xlargewidth": 600, 
       "xlarge": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-articleLarge.jpg", 
       "xlargeheight": 397 
      }, 
      "subType": "xlarge", 
      "crop_name": null 
      }, 
      { 
      "rank": 0, 
      "subtype": "wide", 
      "caption": null, 
      "credit": null, 
      "type": "image", 
      "url": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-thumbWide.jpg", 
      "height": 126, 
      "width": 190, 
      "legacy": { 
       "wide": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-thumbWide.jpg", 
       "widewidth": 190, 
       "wideheight": 126 
      }, 
      "subType": "wide", 
      "crop_name": null 
      }, 
      { 
      "rank": 0, 
      "subtype": "thumbnail", 
      "caption": null, 
      "credit": null, 
      "type": "image", 
      "url": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-thumbStandard.jpg", 
      "height": 75, 
      "width": 75, 
      "legacy": { 
       "thumbnailheight": 75, 
       "thumbnail": "images/2017/12/03/books/review/03GOODMAN1/03GOODMAN1-thumbStandard.jpg", 
       "thumbnailwidth": 75 
      }, 
      "subType": "thumbnail", 
      "crop_name": null 
      } 
     ], 
     "headline": { 
      "main": "Remembering President Obama", 
      "kicker": null, 
      "content_kicker": null, 
      "print_headline": "The 44th", 
      "name": null, 
      "seo": null, 
      "sub": null 
     }, 
     "keywords": [ 
      { 
      "name": "subject", 
      "value": "Books and Literature", 
      "rank": 1, 
      "major": "N" 
      }, 
      { 
      "name": "creative_works", 
      "value": "Obama: The Call of History (Book)", 
      "rank": 2, 
      "major": "N" 
      }, 
      { 
      "name": "persons", 
      "value": "Baker, Peter", 
      "rank": 3, 
      "major": "N" 
      }, 
      { 
      "name": "persons", 
      "value": "Obama, Barack", 
      "rank": 4, 
      "major": "N" 
      }, 
      { 
      "name": "subject", 
      "value": "United States Politics and Government", 
      "rank": 5, 
      "major": "N" 
      }, 
      { 
      "name": "subject", 
      "value": "Presidents and Presidency (US)", 
      "rank": 6, 
      "major": "N" 
      } 
     ], 
     "pub_date": "2017-12-01T17:03:19+0000", 
     "document_type": "article", 
     "new_desk": "BookReview", 
     "section_name": "Book Review", 
     "byline": { 
      "original": "By JAMES GOODMAN", 
      "person": [ 
      { 
       "firstname": "James", 
       "middlename": null, 
       "lastname": "GOODMAN", 
       "qualifier": null, 
       "title": null, 
       "role": "reported", 
       "organization": "", 
       "rank": 1 
      } 
      ], 
      "organization": null 
     }, 
     "type_of_material": "Review", 
     "_id": "5a218b6a95d0e0246f219b14", 
     "word_count": 625, 
     "score": 5.197477, 
     "uri": "nyt://article/78a2b8f7-430c-516e-8b37-cba782db31de" 
     }, 

Antwort

0

Sie müssen das Antwortobjekt navigieren, um die Daten zu erhalten, die Sie suchen, da es nicht auf der obersten Ebene. Mit Blick auf den JSON-Daten würde ich versuchen, diese:

Für die WebUrl

results.response.docs[0].weburl 

Für den Titel

results.response.docs[0].headline.main 
+0

DANK !! Ich glaube, ich musste weggehen und es mir noch einmal ansehen. Wirklich zu schätzen, die Hilfe – mzayas

+0

Wie würde ich durchlaufen alle JSON-Antwortobjekte (jeden Artikel in der Abfrage) – mzayas

+0

Es gibt ein "Antwort" -Objekt, das eine "Docs" -Array von Artikeln enthält. Daher würden Sie die Artikel durch 'docs [i]' durchlaufen, wobei 'i = 0 ... (arraysize-1)'. dh 'docs [0]', dann 'docs [1]', dann 'docs [2]', usw. – dstudeba

Verwandte Themen