Python program to help you find keywords for your blog

The googlesearch-python library is used to perform Google searches, and the beautifulsoup4 library helps in parsing the web pages. Keep in mind that web scraping may vary based on the structure of the websites, and you might need to adjust the code accordingly for different websites.

import tkinter as tk
from tkinter import ttk
from googlesearch import search
import requests
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string
import requests

# Download NLTK resources
import nltk
nltk.download('punkt')
nltk.download('stopwords')

class WebKeywordGeneratorApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Web Keyword Generator")

        self.label = ttk.Label(root, text="Enter your search parameter:")
        self.label.pack(padx=10, pady=10)

        self.entry = ttk.Entry(root, width=40)
        self.entry.pack(padx=10, pady=5)

        self.generate_button = ttk.Button(root, text="Generate Keywords", command=self.generate_keywords)
        self.generate_button.pack(padx=10, pady=10)

        self.keywords_label = ttk.Label(root, text="Generated Keywords:")
        self.keywords_label.pack(padx=10, pady=5)

        self.keywords_text = tk.Text(root, height=10, width=40)
        self.keywords_text.pack(padx=10, pady=5)

    def generate_keywords(self):
        search_param = self.entry.get()
        if search_param:
            keywords = self.extract_keywords_from_web(search_param)
            self.display_keywords(keywords)
        else:
            self.keywords_text.delete("1.0", tk.END)
            self.keywords_text.insert(tk.END, "Please enter a search parameter.")

    def extract_keywords_from_web(self, search_param):
        query = search_param + " keywords"
        search_results = search(query, 5, 5)

        keywords = []
        for url in search_results:
            try:
                page_text = self.get_web_page(url)
                page_keywords = self.extract_keywords(page_text)
                keywords.extend(page_keywords)
            except Exception as e:
                print("Error:", e)

        return keywords

    def get_web_page(self, url):
        try:
            response = requests.get(url)
            return response.text
        except requests.exceptions.RequestException as e:
            print("Error:", e)
            return ""

    def extract_keywords(self, text):
        stop_words = set(stopwords.words('english'))
        words = word_tokenize(text)
        keywords = [word.lower() for word in words if word.lower() not in stop_words and word not in string.punctuation]
        return keywords

    def display_keywords(self, keywords):
        self.keywords_text.delete("1.0", tk.END)
        if keywords:
            self.keywords_text.insert(tk.END, "\n".join(keywords))
        else:
            self.keywords_text.insert(tk.END, "No keywords generated.")

if __name__ == "__main__":
    root = tk.Tk()
    app = WebKeywordGeneratorApp(root)
    root.mainloop()

Copy and paste this code into a .py file and run it using a Python interpreter. The program will open a GUI window where you can enter a search parameter, click the “Generate Keywords” button, and the generated keywords will be displayed in the text box below. Note that this example is quite basic, and you can further enhance and customize it based on your requirements.

Note: This version of the program fixes the issues and should provide a better result when generating keywords from web search results. Remember that the quality of keywords generated depends on the content of the web pages and the search parameter used.

You could do a lot better that this. We need to parse the html generated and see the program how it works. I’ll keep that as a to-do for you.

Next, To generate a nicely formatted HTML output from the program, you can modify the display_keywords method to create an HTML file containing the generated keywords. Here’s how you can do it:

class WebKeywordGeneratorApp:
    # ... (rest of the class code)

    def display_keywords(self, keywords):
        if keywords:
            html_content = "<html>\n<head>\n<title>Generated Keywords</title>\n</head>\n<body>\n"
            html_content += "<h1>Generated Keywords</h1>\n<ul>\n"
            for keyword in keywords:
                html_content += f"<li>{keyword}</li>\n"
            html_content += "</ul>\n</body>\n</html>"

            with open("generated_keywords.html", "w") as html_file:
                html_file.write(html_content)
            
            self.keywords_text.delete("1.0", tk.END)
            self.keywords_text.insert(tk.END, "HTML output generated in 'generated_keywords.html'.")
        else:
            self.keywords_text.delete("1.0", tk.END)
            self.keywords_text.insert(tk.END, "No keywords generated.")

# ... (rest of the program)

if __name__ == "__main__":
    root = tk.Tk()
    app = WebKeywordGeneratorApp(root)
    root.mainloop()
Dhakate Rahul

Dhakate Rahul

Leave a Reply

Your email address will not be published. Required fields are marked *