![](/img/trans.png)
[英]How do i send the data of a form (retrieved from a function of an HTML page) to a function of another HTML page in Django/Python?
[英]How do i output data from python django to html
我正在scraping
ncdc website
,我想output the result
我的HTML
文件中的結果HTML
文件僅呈現last value in the list
這是我的web scraper
代碼
在我的views.py
中,我有這個function
def feed(request):
URL = 'https://ncdc.gov.ng/news/press'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='example')
news_elems = results.find_all('div', class_='col-sm-10')
for news_elem in news_elems:
title_elem = news_elem.find('h3')
date_elem = news_elem.find('h4')
# sub_elem = news_elem.find(id='text')
link_elem = news_elem.find('a', class_='white-text')
if None in (title_elem, date_elem, link_elem):
continue
title = print(title_elem.text.strip())
date = print(date_elem.text.strip())
link = print(link_elem.get('href'))
space = print()
title = title_elem.text.strip(),
date = date_elem.text.strip(),
link = link_elem.get('href')
context = {'title': title, 'date': date, 'link': link, 'space': space}
return render(request, 'feed.html', context)
這是我的提要。html 代碼
<div class="card card-primary card-outline">
<div class="card-header">
<h5 class="card-title m-0">News Feed</h5>
</div>
<div class="card-body">
<h6 class="card-title">{{title}}</h6>
<p class="card-text">{{date}}</p>
<a href="https://ncdc.gov.ng{{link}}" class="btn btn-primary">Read More</a>
</div>
</div>
output 僅返回last value in the list of tuples
。 我希望它返回列表中each item
return the title, date and link
。
您的問題是您正在替換for
循環中的上下文,因此循環后的context
將僅包含最后一個。 所以 scaper 應該是這樣的:
def feed(request):
URL = 'https://ncdc.gov.ng/news/press'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='example')
news_elems = results.find_all('div', class_='col-sm-10')
data = []
for news_elem in news_elems:
title_elem = news_elem.find('h3')
date_elem = news_elem.find('h4')
link_elem = news_elem.find('a', class_='white-text')
if None in (title_elem, date_elem, link_elem):
continue
title = print(title_elem.text.strip())
date = print(date_elem.text.strip())
link = print(link_elem.get('href'))
space = print()
title = title_elem.text.strip(),
date = date_elem.text.strip(),
link = link_elem.get('href')
# this line!!!
data.append({'title': title, 'date': date, 'link': link, 'space': space})
return render(request, 'feed.html', context={'data': data})
並在您的模板中:
{% for d in data %}
<div class="card card-primary card-outline">
<div class="card-header">
<h5 class="card-title m-0">News Feed</h5>
</div>
<div class="card-body">
<h6 class="card-title">{{d.title}}</h6>
<p class="card-text">{{d.date}}</p>
<a href="https://ncdc.gov.ng{{d.link}}" class="btn btn-primary">Read More</a>
</div>
</div>
{% endfor %}
在上面的代碼中,每次都會重新編寫“上下文”。 所以它得到了“news_elems”中的最后一個值。
在 for 循環上方創建“上下文”列表。 Append for 循環中的每個“new_element”。
您的context
變量 - 您在each iteration
中都被覆蓋。
而是在loop
之前創建一個名為contexts
的變量: contexts=[]
和append
loop
內的context
到該contexts list
: contexts.append({'title': title, 'date': date, 'link': link, 'space': space})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.