Saurav Sharma
Python Django Blog


Python Django Blog

Extract Keyword from a Website using Python

Photo by Ilya Pavlov on Unsplash

Extract Keyword from a Website using Python

Saurav Sharma's photo
Saurav Sharma
·Oct 17, 2022·

1 min read

Play this article
Run this command in Terminal after activating environment:
python -c "import nltk;'stopwords')"
python -c "import nltk;'punkt')"

from bs4 import BeautifulSoup
import requests
from rake_nltk import Rake

rake = Rake()  # ML library to extract keywords from text

url_to_scrape = ""

def get_text(url):
    # get the html content
    html_content = requests.get(url).text
    # parse the html content
    soup = BeautifulSoup(html_content, "html.parser")
    return soup.get_text()

website_text = get_text(url_to_scrape)
keywords = rake.get_ranked_phrases_with_scores()

for score, keyword in keywords:
    print(f"{score} -> {keyword}")
Share this