You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blogger API:如何读取特定页面文本及JSON文件内容

Alright, let's tackle your two tech needs step by step. I'll keep things practical with code examples you can adapt right away.

1. Reading Specific Page Content via the Blogger API

First off, you'll need to have the Blogger API enabled in your Google Cloud project, plus either an API key (for public blogs) or OAuth 2.0 credentials (for private/restricted content). Once that's sorted, here's how to pull a specific page's text:

  • Find your blog ID and page ID:
    Head to your Blogger dashboard, navigate to the target page, and check the URL. It’ll look like https://www.blogger.com/blog/posts/[BLOG_ID]?pageID=[PAGE_ID]—jot down both values.
  • Construct the API request:
    Use the pages.get endpoint. For public blogs, an API key is enough for authentication.

Example (Python)

import requests

# Replace these with your actual values
BLOG_ID = "your-blog-id-here"
PAGE_ID = "your-page-id-here"
API_KEY = "your-api-key-here"

# API endpoint URL
url = f"https://www.googleapis.com/blogger/v3/blogs/{BLOG_ID}/pages/{PAGE_ID}?key={API_KEY}"

response = requests.get(url)
if response.status_code == 200:
    page_data = response.json()
    # Extract the page's content (returns HTML by default)
    page_content = page_data.get("content")
    print("Page Content:\n", page_content)
else:
    print(f"Error fetching page: {response.status_code} - {response.text}")

If you need plain text instead of HTML, use a library like BeautifulSoup to strip tags:

from bs4 import BeautifulSoup

plain_text = BeautifulSoup(page_content, "html.parser").get_text()
print("Plain Text Content:\n", plain_text)
2. Reading Content from a Specified JSON File

Since you mentioned you already have working code, I’ll focus on straightforward, no-fuss methods for reading JSON files across common languages:

Python (Built-in Module)

Use the native json module—no external dependencies needed:

import json

# Replace with your JSON file path
json_file_path = "path/to/your/file.json"

try:
    with open(json_file_path, "r") as file:
        json_data = json.load(file)
        # Access specific fields as needed
        print("JSON Data:\n", json_data)
        # Example: Pull a "description" field
        # print("Description:", json_data.get("description"))
except FileNotFoundError:
    print(f"Error: The file {json_file_path} was not found.")
except json.JSONDecodeError:
    print("Error: The file contains invalid JSON.")

Node.js (Server-Side/Local Scripts)

Use the built-in fs module with async/await for clean code:

const fs = require('fs').promises;

async function readJsonFile(filePath) {
    try {
        const fileContent = await fs.readFile(filePath, 'utf8');
        const jsonData = JSON.parse(fileContent);
        console.log('JSON Data:', jsonData);
        // Example: Access a "title" key
        // console.log('Title:', jsonData.title);
    } catch (err) {
        console.error('Error reading JSON file:', err.message);
    }
}

// Replace with your file path
readJsonFile('path/to/your/file.json');

Browser-Side JavaScript

If you’re loading a local JSON file via user input in the browser:

function handleFileSelect(event) {
    const file = event.target.files[0];
    if (!file) return;

    const reader = new FileReader();
    reader.onload = function(e) {
        try {
            const jsonData = JSON.parse(e.target.result);
            console.log('JSON Data:', jsonData);
        } catch (err) {
            console.error('Invalid JSON:', err);
        }
    };
    reader.readAsText(file);
}

// Attach this to a file input element:
// <input type="file" accept=".json" onchange="handleFileSelect(event)">

内容的提问来源于stack exchange,提问作者ArghadipDasCEO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:59:58