import time
import requests
import pandas as pd
import openpyxl

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}

url = "https://......../wp-json/wp/v2/posts"
start_post_id = 0
end_post_id = 1000000

post_data = []

post_id = start_post_id

while post_id <= end_post_id:
    res = requests.get(f"{url}/{post_id}", headers=headers)
    if res.status_code == 200:
        post = res.json()
        if 'title' in post and 'content' in post:
            post_data.append({
                "title": post['title']['rendered'],
                "content": post['content']['rendered']
            })
            print(f"Post ID {post_id} çekildi ve kaydedildi.")
        else:
            print(f"Post ID {post_id} çekilemedi. Hata kodu:", res.status_code)
    else:
        print(f"Post ID {post_id} çekilemedi. Hata kodu:", res.status_code)

    post_id += 1
    time.sleep(3)

# Create a DataFrame from the post_data list
df = pd.DataFrame(post_data)

# Save the DataFrame to an Excel file with 'title' and 'content' in separate columns
excel_file = "post_data.xlsx"
with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:
    df.to_excel(writer, index=False, sheet_name='Sheet1')
    writer.book.save()

print(f"Veriler Excel dosyasına kaydedildi: {excel_file}")