Web Crawler with Django view.py

1

I am doing a simple web crawler, using django 2.0, I want to capture only the "title" class of the news and then render "return render" to a simple html, below my view.py. At the moment I'm using the "Return HttpRensonse". how can I get the data and render to an html?

from django.shortcuts import render
import requests
from bs4 import BeautifulSoup
from lxml import html
import urllib3

from django.http import HttpResponse #Para apenas fazer testes testes apenas

def crawler(request):
    url = "https://noticias.uol.com.br/"
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html, "html.parser")
    return HttpResponse(soup)
    
asked by anonymous 31.05.2018 / 03:08

1 answer

0

According to the Django documentation , how to return an object HttpResponse with a template and context is:

from django.http import HttpResponse
from django.template import loader

def my_view(request):
    # View code here...
    t = loader.get_template('myapp/index.html')
    c = {'foo': 'bar'}
    return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')
    
13.06.2018 / 09:58