Web Crawler with Django’s view.py

Asked

Viewed 114 times

1

I am making 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. I am currently using "Return Httprensonse". how can I take 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)

1 answer

1

According to the documentation of Django, the way to return an Object HttpResponse with a template and context:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.