Flask Dynamic data update without reload page

前端 未结 1 1545
走了就别回头了
走了就别回头了 2021-01-29 22:53

i\'m trying to create something like Google Suggest Tool (via suggest api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query )

I\'m l

1条回答
  •  借酒劲吻你
    2021-01-29 23:15

    Working example:

    app.py

    from flask import Flask, render_template, request
    import requests
    from bs4 import BeautifulSoup
    
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    
    @app.route('/suggestions')
    def suggestions():
        text = request.args.get('jsdata')
    
        suggestions_list = []
    
        if text:
            r = requests.get('http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q={}&gl=in'.format(text))
    
            soup = BeautifulSoup(r.content, 'lxml')
    
            suggestions = soup.find_all('suggestion')
    
            for suggestion in suggestions:
                suggestions_list.append(suggestion.attrs['data'])
    
            #print(suggestions_list)
    
        return render_template('suggestions.html', suggestions=suggestions_list)
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    index.html

    
    
    
    
    
        Suggestions
    
    
    
    
    Search: 
    
    

    suggestions.html

    
    

    0 讨论(0)
提交回复
热议问题