简体   繁体   English

如何使用 BeautifulSoup 和 Flask 从循环中打印所有字典值

[英]How to print all dictionary values from loop using BeautifulSoup and Flask

def search():
    query = "xxx"
    r = requests.get('website?q=' + query)
    soup = BeautifulSoup(r.content, 'lxml')
    results=[]
    for row in soup.findAll('li', attrs = {'class':'res'}):
        result = {}
        result['urltext'] = row.a.text
        result['url'] = row.cite.text
        results.append(result)

        return render_template('site.html', urll = result['url'], urltext = result['urltext'])
    else:
        return render_template('start.html')

When used like that i am getting single result in site.html, it's like the loop is stopping and getting first result and passing it to the site.html.当这样使用时,我在 site.html 中得到单个结果,就像循环正在停止并获取第一个结果并将其传递给 site.html。 And i want all results it finds to be passed.我希望它找到的所有结果都通过。

def search():
    query = "xxx"
    r = requests.get('website?q=' + query)
    soup = BeautifulSoup(r.content, 'lxml')
    results=[]
    for row in soup.findAll('li', attrs = {'class':'res'}):
        result = {}
        result['urltext'] = row.a.text
        result['url'] = row.cite.text
        results.append(result)

        print (result['url'])

Using print gives me all results.使用打印给了我所有的结果。 But that is pointless, unless i will drop the render_template idea and print whole HTML with result['url'] and result['urltext'] in it somehow.但这毫无意义,除非我将放弃render_template 的想法并以某种方式打印带有result['url']result['urltext'] 的整个 HTML。

Your problem is return which always ends function at once so next loop will be never executed.您的问题是return总是立即结束函数,因此永远不会执行下一个循环。 So don't use return inside for -loop.所以不要在for循环中使用return

And using else with for is also wrong idea because else will be executed only when you use break in for -loo.并且将elsefor一起使用也是错误的想法,因为else仅在您使用break in for -loo 时才会执行。 for/else doesn't work like if/else . for/else不像if/else那样工作。

Another problem is result['url'] and result['urltext'] in render_template - it will have only last value.另一个问题是render_template result['url']result['urltext'] - 它只有最后一个值。 You have all values in results , not in result['url'] and result['urltext'] .您拥有results所有值,而不是result['url']result['urltext'] Maybe you should send it as也许你应该把它作为

return render_template('site.html', results=results)

and inside template use和内部模板使用

{% for row in results %}
   <a href="{{ row.url }}">{{ row.ulrtext }}</a>
{% endfor %}

def search():
    query = "xxx"
    r = requests.get('website?q=' + query)
    soup = BeautifulSoup(r.content, 'lxml')
    results = []
    for row in soup.findAll('li', attrs={'class': 'res'}):
        result = {}
        result['urltext'] = row.a.text
        result['url'] = row.cite.text
        results.append(result)

    if results:
        return render_template('site.html', results=results)
    else:
        return render_template('start.html')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM