Solution 1 :

your problem is that you are replacing your context in your for loop, so the context after loop will only contain last one. so scaper should be like:

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})

and in your template:

{% 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 %}

Solution 2 :

In the above code, “context” is re written every time. So it get’s the last value present in “news_elems”.

Create “context” list above for loop. Append each “new_element” in for loop.

Solution 3 :

Your context variable – you are overwriting in each iteration.

Instead create a variable called contexts before the loop : contexts=[]

And append the context inside the loop to that contexts list : contexts.append({'title': title, 'date': date, 'link': link, 'space': space})

Problem :

I am scraping the ncdc website and I want to output the result in my HTML file
The HTML file is rendering only the last value in the list

Here is my web scraper code

In my views.py I have this 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)

this is my feed.html code

                <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>

The output is only returning the last value in the list of tuples.
I want it to return the title, date and link for each item in the list.

Comments

Comment posted by Azer

You need to create a list with items that all have title, date, and link. Then you can loop through the list in your template file with a

Comment posted by dMd

it’s adding the brackets, is there a way i can remove the brackets in the output e.g (‘Nigeria Releases COVID-19 Guide to Re-Open Places of Worship’,) (‘Sat 13 Jun 2020’,)

Comment posted by aasmpro

@dMd i don’t know how that happened, so it may be because of your data are tuples so you may use something like

By