循环通过其他循环,我正在尝试从上下文中插入数据。在这种情况下,我该如何避免循环其他循环。我该如何修复它?views.py
我正在使用beautifulsoup从wiki中获取数据。正在上传图像。
它返回以下内容:
模板.html
{%extends 'profilecontent/base.html' %}
{%block content%}
<div class="container-home">
<div class="home-wrapper home-wrapper-first">
<p class='home-matches'>Przyszłe mecze <span class='home-week'>W3 D2</span></p>
<div class="match-wrapper">
<table>
{%for second_team in second_team_names%}
{%for first_team in first_team_names %}
{%for hour_game in hours_games%}
<tr><td>{{first_team}}</td><td>{{hour_game}}</td><td>{{second_team}}</td></tr>
{%endfor%}
{%endfor%}
{%endfor%}
</table>
</div>
</div>
<div class="home-wrapper home-wrapper-second">
</div>
</div>
{%endblock%}
views.py
from bs4 import BeautifulSoup
import requests
def home(request):
source = requests.get('https://lol.gamepedia.com/Ultraliga/Season_3').text
soup = BeautifulSoup(source, 'lxml')
first_team_names = []
second_team_names = []
td1s = None
td2s = None
hours_games = ['17:30', '18:30', '19:30', '20:30']
tables = soup.find_all('table', class_='wikitable matchlist')
for table in tables:
td1s = table.find_all('td', class_='matchlist-team1')
td2s = table.find_all('td', class_='matchlist-team2')
for td in td1s:
span = td.find('span')
first_team_names.append(span.text)
for td in td2s:
span = td.find('span')
second_team_names.append(span.text)
context = {
'first_team_names':first_team_names,
'second_team_names':second_team_names,
'hours_games':hours_games,
}
return render(request, 'profilecontent/home.html', context)
森栏
相关分类