Got it! Since you're running this on a Windows VM, you can set up the automation using Windows Task Scheduler. Below is a complete guide to scraping questions from Reddit, rewriting them using ChatGPT, and posting them to your WordPress blog automatically every day.
📌 Full Setup Guide
1. Install Required Python Libraries
First, install the necessary libraries:
pip install requests beautifulsoup4 openai schedule
2. Python Script (daily_script.py
)
This script will:
- Scrape Reddit for questions.
- Reword them and generate answers using ChatGPT.
- Post them to your WordPress blog.
import requests
from bs4 import BeautifulSoup
import openai
import json
import time
# 🔹 SET YOUR API KEYS
openai.api_key = "YOUR_OPENAI_API_KEY"
wp_url = "https://hubs.uk/wp-json/wp/v2/posts"
wp_user = "your_username"
wp_password = "your_app_password"
# 🔹 FUNCTION: SCRAPE QUESTIONS FROM REDDIT
def scrape_reddit_questions():
url = "https://www.reddit.com/r/TechSEO/"
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
questions = []
for post in soup.find_all("h3"): # Reddit post titles are inside <h3> tags
title = post.get_text()
if "?" in title: # Filtering only questions
questions.append(title)
return questions[:5] # Get the first 5 questions
# 🔹 FUNCTION: REWORD & ANSWER USING CHATGPT
def reword_and_answer(question):
prompt = f"Reword this question: '{question}'.\nThen, provide a detailed answer."
response = openai.ChatCompletion.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response['choices'][0]['message']['content']
# 🔹 FUNCTION: POST TO WORDPRESS
def post_to_wordpress(title, content):
data = {
"title": title,
"content": content,
"status": "publish" # Change to "draft" if you want to review before publishing
}
response = requests.post(wp_url, json=data, auth=(wp_user, wp_password))
if response.status_code == 201:
print(f"✅ Successfully posted: {title}")
else:
print(f"❌ Error: {response.text}")
# 🔹 MAIN FUNCTION
def main():
print("🔍 Scraping questions from Reddit...")
questions = scrape_reddit_questions()
for question in questions:
print(f"🔹 Processing: {question}")
qa_text = reword_and_answer(question)
# Extract reworded question and answer
parts = qa_text.split("\n", 1) # Split into question + answer
if len(parts) == 2:
new_question, answer = parts
else:
new_question, answer = question, qa_text # Fallback
print(f"📝 Posting: {new_question}")
post_to_wordpress(new_question, answer)
# Run the script
if __name__ == "__main__":
main()
3. Schedule It to Run Daily on Your Windows VM
Since your Windows VM will need to run this script automatically, you can use Task Scheduler:
📌 Steps to Schedule the Script
Open Task Scheduler:
Press Win + R
, type taskschd.msc
, and hit Enter.
Create a New Task:
- Click "Create Basic Task".
- Name it "Reddit to WordPress Automation".
- Click Next.
Set the Trigger:
- Choose Daily.
- Set the time (e.g., 8:00 AM every day).
- Click Next.
Set the Action:
🎯 Summary
✅ Scrapes questions from Reddit
✅ Rewords and answers them using ChatGPT
✅ Posts them to WordPress
✅ Runs automatically every day on your Windows VM
🚀 Want to test it now? Just run daily_script.py
manually before scheduling!