簡體   English   中英

我如何從 python django 到 html 的 output 數據

[英]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 listcontexts.append({'title': title, 'date': date, 'link': link, 'space': space})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM