0

我正在为书籍构建一个推荐系统,所以我尝试从 google api 获取推荐的书籍图像

这是我的 get_image()

def get_image(books):
    img=[]
    for i in range(len(books)):
        response=requests.get(url + books[i])
        obj=json.loads(response.text)
        for i in obj["items"]:
            try: 
                w= repr(i["volumeInfo"]["imageLinks"]["smallThumbnail"])
                img.append(w)
            except:
                pass
        return img  

但是当我尝试在 jinja 模板中显示时,它显示以下错误

127.0.0.1 - - [15/Jun/2021 10:40:35] "GET /'http://books.google.com/books/content?id=9GtPBNpxOu0C&printsec=frontcover&img=1&zoom=5&edge
=curl&source=gbs_api%27 HTTP/1.1" 404 -
127.0.0.1 - - [15/Jun/2021 10:40:35] "GET /'http://books.google.com/books/content?id=fwIuAQAAIAAJ&printsec=frontcover&img=1&zoom=5&sour
ce=gbs_api%27 HTTP/1.1" 404 -
127.0.0.1 - - [15/Jun/2021 10:40:35] "GET /'http://books.google.com/books/content?id=i5DzFhBT3mIC&printsec=frontcover&img=1&zoom=5&edge
=curl&source=gbs_api%27 HTTP/1.1" 404 -
127.0.0.1 - - [15/Jun/2021 10:40:35] "GET /'http://books.google.com/books/content?id=K6dc6k4oOd4C&printsec=frontcover&img=1&zoom=5&edge
=curl&source=gbs_api%27 HTTP/1.1" 404 -
127.0.0.1 - - [15/Jun/2021 10:40:35] "GET /'http://books.google.com/books/content?id=Q1HKGG5PGw4C&printsec=frontcover&img=1&zoom=5&edge
=curl&source=gbs_api%27 HTTP/1.1" 404 -

这是我的搜索功能,我已将参数传递给我的 read.html

@app.route('/search',methods=['GET','POST'])
def search():
    choice = request.args.get('search')
    # removing all the characters except alphabets and numbers.
    # passing the choice to the recommend() function
    # passing the choice to the recommend() function
    books = recommend(choice)
    image=get_image(books)
    # if rocommendation is a string and not list then it is else part of the
    # recommend() function.
    if type(books) == type('string'):
        return render_template('read.html', book=books,image=image,s="oppps")
    else:
        return render_template('read.html', book=books,image=image)

这是我的read.html,我试图在其中显示图像和名称

<div class="row">
    <div class="col-sm-4">
        <div class="card" style="width: 200px; background-color: black;">
            <img class="card-img-top" src="{{ image[0] }}" alt="k" style="width: 180px;">
            <div class="card-body">
                <h5 class="card-title">{{ book[0] }}</h5>
            </div>
        </div>
    </div>

    <div class="col-sm-4">
        <div class="card" style="width: 200px; background-color: black;">
            <img class="card-img-top" src="{{ image[1] }}" alt="k" style="width: 180px;">
            <div class="card-body">
                <h5 class="card-title">{{ book[1] }}</h5>
            </div>
        </div>
    </div>

    <div class="col-sm-4">
        <div class="card" style="width: 200px; background-color: black;">
            <img class="card-img-top" src="{{ image[2] }}" alt="k" style="width: 180px;">
            <div class="card-body">
                <h5 class="card-title">{{ book[2] }}</h5>
            </div>
        </div>
    </div>
</div>

我试图捕捉错误但无法显示,请你帮我解决

4

0 回答 0