Advertisement
shoaib-santo

Keyword Count Analysis [Streamlit]

Oct 26th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import pandas as pd
  4. import re
  5. import streamlit as st
  6.  
  7. # Function to get the webpage content
  8. def get_webpage_content(url):
  9.     try:
  10.         response = requests.get(url)
  11.         response.raise_for_status()  # Raise an error for bad responses
  12.         return response.text
  13.     except requests.exceptions.RequestException as e:
  14.         st.error(f"Error fetching the URL: {e}")
  15.         return None
  16.  
  17. # Function to read keywords from the uploaded file
  18. def read_keywords_from_file(file):
  19.     try:
  20.         keywords = [line.decode('utf-8').strip().lower() for line in file if line.strip()]
  21.         return keywords
  22.     except Exception as e:
  23.         st.error(f"Error reading keywords file: {e}")
  24.         return []
  25.  
  26. # Function to count keyword occurrences accurately using regular expressions
  27. def count_keywords(content, keywords):
  28.     # Remove HTML tags using BeautifulSoup
  29.     soup = BeautifulSoup(content, 'html.parser')
  30.     text = soup.get_text().lower()  # Convert text to lowercase for case-insensitive search
  31.  
  32.     # Dictionary to store keyword counts
  33.     keyword_count = {}
  34.  
  35.     # Count occurrences of each keyword using regex
  36.     for keyword in keywords:
  37.         # Create a regex pattern to match the keyword as a whole word
  38.         pattern = rf'\b{re.escape(keyword)}\b'
  39.         count = len(re.findall(pattern, text))
  40.         keyword_count[keyword] = count
  41.  
  42.     return keyword_count
  43.  
  44. # Streamlit UI setup
  45. st.title("Keyword Count Analysis")
  46.  
  47. # URL input
  48. url = st.text_input("Enter the URL to analyze: ")
  49.  
  50. # File upload for keywords
  51. uploaded_file = st.file_uploader("Upload a file containing keywords (one per line)", type=['txt'])
  52.  
  53. # Main logic execution
  54. if st.button("Analyze Keywords") and uploaded_file:
  55.     # Read keywords from uploaded file
  56.     keywords = read_keywords_from_file(uploaded_file)
  57.  
  58.     # Fetch the webpage content
  59.     content = get_webpage_content(url)
  60.  
  61.     if content and keywords:
  62.         # Count keywords
  63.         keyword_count = count_keywords(content, keywords)
  64.  
  65.         # Display results as a DataFrame
  66.         df = pd.DataFrame(list(keyword_count.items()), columns=['Keyword', 'Count'])
  67.         st.write("Keyword Counts:")
  68.         st.dataframe(df)
  69.  
  70.         # Download option for results as CSV
  71.         csv = df.to_csv(index=False).encode('utf-8')
  72.         st.download_button("Download Results as CSV", data=csv, file_name="keyword_counts.csv", mime="text/csv")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement