IFRAME SYNC
import requests
from bs4 import BeautifulSoup
from collections import Counter
import re
def get_text_from_url(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
paragraphs = soup.find_all('p')
text = ' '.join([p.get_text() for p in paragraphs])
return text
except Exception as e:
print(f"Error fetching content from {url}: {e}")
return None
def calculate_word_frequency(text):
words = re.findall(r'\b\w+\b', text.lower())
word_frequency = Counter(words)
return word_frequency
def calculate_article_density(text, keyword):
words = re.findall(r'\b\w+\b', text.lower())
keyword_count = words.count(keyword.lower())
total_words = len(words)
if total_words == 0:
return 0
density = (keyword_count / total_words) * 100
return density
def main():
url = input("Enter the URL of the article: ")
keyword = input("Enter the keyword to check density: ")
article_text = get_text_from_url(url)
if article_text:
density = calculate_article_density(article_text, keyword)
print(f"The density of the keyword '{keyword}' in the article is: {density:.2f}%")
if __name__ == "__main__":
main()
IFRAME SYNC
Comments
Post a Comment