Forums

How to convert python loop variable output into jinja?

basically I need this to display on the url:

i = 0
    while i < 15:        
        tags1 = soup.find_all('h2')[i].get_text()
        print(tags1)
        if i == 3:
            break
        i = i + 1

currently, only works in python idle

any help would be great

full code:

import requests
from bs4 import BeautifulSoup
import json


def rurls():
    url = "https://myracing.com/racecards/"
    response = requests.get(url)
    data = response.text
    soup = BeautifulSoup(data, 'lxml')
    tags1 = soup.find_all('a', class_='RacesTable__meeting-name')

    i = 0
    while i < 15:        
        tags1 = soup.find_all('h2')[i].get_text()
        print(tags1)
        if i == 3:
            break
        i = i + 1


print(rurls())

ok, this seems to work now:

i = 0
    meetings = []
    while i < 15:
        tags1 = soup.find_all('h2')[i].get_text()
        meetings.append(tags1)
        if i == 3:
            break
        i = i + 1
        x = meetings
#print(rurls())
    #x = rurls2()
    return render_template('meetings.html', x=x

)

just need to tidy the output up a little now, if anyone knows a better way that would be cool

for instance the output is as such:

Racing: meetings today

['\n\n Newcastle Tips \n', '\n\n 
Kempton (AW) Tips \n', '\n\n
 Plumpton Tips \n', '\n\n
 Wolverhampton (AW) Tips \n'

how to tidy this up? (ie. get rid of the \n' parts)

https://madmartin.pythonanywhere.com/meetings

String objects have plenty of methods that can help you. See https://docs.python.org/3.8/library/stdtypes.html#str