In this article, I'd like to share with you my simple Django code to get the h1 tag value from any website.
we need to install beautifulsoup and requests libraries.

1. Installing libraries

install requests


pip install requests

install beautifulsoup


pip install beautifulsoup4

2. Getting h1 tag value by using Django and beautifulsoup

views.py


from bs4 import BeautifulSoup
import requests

def dj_bs(request):
    if request.method == "POST":
        website_link = request.POST.get('web_link', None)

        #requests
        url = website_link #url
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}#headers
        source=requests.get(url, headers=headers).text # url source
        
        #beautifulsoup
        soup = BeautifulSoup(source, 'html.parser')
        h1_val = soup.h1.string #h1 value

        return render(request, 'django-bs.html', {'h1_val':h1_val})

    return render(request, 'django-bs.html')

In the above example, we've used Requests library to get the source code of the link, then we've find h1 value by using beautifulsoup, then we've used Beautifulsoup to get h1 tag value


urls.py


path('django-bs/', dj_bs, name="django_bs"),


django-bs.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<html>
    <head>
        <title>django and Bs</title>
    </head>
    <body>
   <!-- form -->
<form action="{% url 'django_bs' %}", method="POST">
    {% csrf_token %}
    <input type="text" name="web_link">
    <input type="submit" name="submit">
    

    <!-- output -->
    <div class="output">
        <h2>{{h1_val}}</h2>
    </div>


    </body></html>

output

working with django, beautifulsoup and requests

English today is not an art to be mastered it's just a tool to use to get a result