NYTimes文章搜索API - UndefinedError:' unicode对象'没有属性' repsonse'

时间:2017-12-28 18:58:56

标签: python json unicode

我在NYTimes Article Search API上遇到了一些问题。 我正在使用python包" NYTimesArtcile"我试图使用一个简单的例子,这样我就可以熟悉将数据传递到NYTimes的网页。

这是我的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)

我在这里查看"精选页面":

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

这是我的html页面,features.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 %}

我已经在jinja formate中放置了results.title(),因为它是唯一有效的东西。当我返回results.title()时,我从JSON数据中获得前3个对象(状态,响应,版权)。我想要的是文章的标题和web_url。

我知道我想要的信息是在results.response.docs中,但是当我尝试这个时,我得到了上面的错误。

这里有一些纽约时报的JSON(一篇文章)。任何帮助表示赞赏:

{
  "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"
      },

1 个答案:

答案 0 :(得分:0)

您需要导航响应对象以获取您要查找的数据,因为它不在顶层。看看JSON数据,我会试试这个:

对于webURL

results.response.docs[0].weburl

标题

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