0

此视图的目的是通过一些计算从 goodread.com 和我的数据库中读取后提取并返回 JSON 格式。

我尝试了很多方法来呈现 API,但仍然没有希望

在查询每个列出的 SQL 查询时,我尝试将用户输入oneIsbn更改为“oneIsbn” 。我在浏览器上得到的就是这样

{ "Error": "Invalid ISBN 0380795272" }

我的代码片段

@app.route("/api/<oneIsbn>", methods=["GET", "POST"])
@login_required
def api(oneIsbn):
    """Returns in JSON format for a single Book"""

    if request.method == "GET":

        check = db.execute("SELECT * FROM books WHERE isbn= :isbn",
                           {"isbn": oneIsbn}).fetchone()
        if check is None:
               return jsonify({"Error": f"Invalid ISBN {oneIsbn}"}), 405
        else:
            res = requests.get(
                "https://www.goodreads.com/book/review_counts.json",
                params={
                    "key": "x9fJg",
                    "isbns": oneIsbn})
            if res.status_code != 200:
                raise Exception("ERROR: API request unsuccessful.")
            else:
                data = res.json()
                y = data["books"][0]["work_reviews_count"]
                r = data["books"][0]["average_rating"]

            isbn = db.execute("SELECT isbn FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            title = db.execute("SELECT title FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            author = db.execute("SELECT author FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            year = db.execute("SELECT year FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            x = db.execute("SELECT COUNT(reviews) FROM reviews WHERE isbn = :isbn",
                                    {"isbn": 'oneIsbn'}).fetchone()
            z = db.execute("SELECT rating FROM reviews WHERE isbn = :isbn",
                                    {"isbn": oneIsbn}).fetchone()

            rev = int(y)
            revCount = int(x.count)
            bothReviewValue = sum((revCount,rev))

            # listRate = float(z)
            rat = float(r)
            bothRatingValue = sum([z,rat]) / 2

            return jsonify(
                ISBN=isbn,
                TITLE=title,
                AUTHOR=author,
                YEAR=year,
                REVIEWS=bothReviewValue,
                RATING=bothRatingValue
                ), 422 

追溯

TypeError
TypeError: unsupported operand type(s) for +: 'int' and 'RowProxy'

Traceback (most recent call last)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
Open an interactive python shell in this frameraise value
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Beacon\Desktop\THINKFUL DOCS\PYTHON\PYTHON WORKSPACE\project1\application.py", line 39, in wrapped_view
return view(**kwargs)
File "C:\Users\Beacon\Desktop\THINKFUL DOCS\PYTHON\PYTHON WORKSPACE\project1\application.py", line 233, in api
bothRatingValue = sum([z,rat]) / 2
TypeError: unsupported operand type(s) for +: 'int' and 'RowProxy'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.
4

1 回答 1

0

我用的这个方法真的很管用。我已经错过了类似的东西

sum method应放入tuple不中,list拉出string率应投至float

            rev = int(y)
            revCount = int(x.count)
            bothReviewValue = sum((revCount,rev))

            listRate = float(z)
            rat = float(r)
            bothRatingValue = sum((listRate,rat)) / 2
于 2020-06-13T21:42:17.717 回答