您好,请帮助我提供此代码?尝试运行脚本时得到UnboundLocalError: local variable 'content' referenced before assignment
。我尝试了不同的缩进,但仍然出错。
它仅适用于2个for循环,因此for title
和for price
非常感谢
def tickets(request):
ticket_offer = []
url = 'some random URL'
result = requests.get(url)
content_data = BeautifulSoup(result.content,features="html.parser")
for title in content_data.find_all('div',{"class": "singleTipTitle"}):
for price in title.find_all('span',{"class": "price"}):
for time in price.find_all('div',{"class": "meta"}):
ticket = {
'title': title.text,'price': price.text,'time': time.text
}
ticket_offer.append(ticket)
content = {'ticket_offer': ticket_offer}
print(ticket_offer)
return render(request,'tickets/tickets.html',content)