Fake Blog Posts with Faker

Creating realistic blog post content for CMS testing or development

Python
from faker import Faker
import random
from datetime import timedelta

fake = Faker()

def generate_blog_post():
    publish_date = fake.date_time_this_year()
    return {
        "title": fake.catch_phrase(),
        "author": fake.name(),
        "publish_date": publish_date.isoformat(),
        "content": fake.paragraphs(nb=random.randint(3, 7)),
        "tags": fake.words(nb=random.randint(2, 5)),
        "comments": [
            {
                "user": fake.user_name(),
                "content": fake.paragraph(),
                "date": (publish_date + timedelta(days=random.randint(1, 30))).isoformat()
            } for _ in range(random.randint(0, 5))
        ],
        "views": random.randint(100, 10000),
        "likes": random.randint(10, 1000)
    }

# Generate 5 blog posts
blog_posts = [generate_blog_post() for _ in range(5)]

# Print the generated blog posts
for post in blog_posts:
    print(f"Title: {post['title']}")
    print(f"Author: {post['author']}")
    print(f"Published: {post['publish_date']}")
    print(f"Content: {' '.join(post['content'])[:200]}...")  # Show first 200 characters
    print(f"Tags: {', '.join(post['tags'])}")
    print(f"Comments: {len(post['comments'])}")
    print(f"Views: {post['views']}")
    print(f"Likes: {post['likes']}")
    print("---")
Click Run or press shift + ENTER to run code.