Back to catalog

niv-bible-fetch

Fetch an NIV Bible chapter in Joseph's study format.

Category ✍️ Writing

NIV Bible Fetch

Fetch a specific chapter from the NIV Bible (via BibleGateway.com) and format it in Joseph's Bible study format.

Joseph's Format

### {verse number}
> {verse text}
-

Each verse: h3 with number, blockquote with text, hyphen separator, blank line between verses.

Workflow

Step 1: Navigate to BibleGateway

browser_navigate → https://www.biblegateway.com/passage/?search={Book}+{Chapter}&version=NIV

Book names should be in full English (e.g., "Philippians", "John", "Genesis").

Step 2: Extract verses via JavaScript

Use browser_console with this extraction script:

(() => {
  const container = document.querySelector('.passage-text');
  if (!container) return {error: 'no passage-text container'};
  
  const verses = [];
  let currentNum = null;
  let currentText = '';
  
  function walk(node) {
    if (node.nodeType === 3) {
      if (currentNum !== null) {
        currentText += node.textContent;
      }
    } else if (node.nodeName === 'SUP' || node.nodeName === 'SUPERSCRIPT') {
      const num = parseInt(node.textContent.trim());
      if (num > 0) {
        if (currentNum !== null && currentText.trim()) {
          verses.push({num: currentNum, text: currentText.trim()});
        }
        currentNum = num;
        currentText = '';
      }
    } else if (node.nodeName === 'H3' || node.nodeName === 'H4') {
      return;
    } else {
      for (const child of node.childNodes) {
        walk(child);
      }
    }
  }
  
  // Handle verse 1: first paragraph often has "4Therefore..." format
  const firstP = container.querySelector('p');
  if (firstP) {
    const text = firstP.textContent.trim();
    const match = text.match(/^\d+(.+)/);
    if (match) {
      verses.push({num: 1, text: match[1].trim()});
    }
  }
  
  let skipFirst = verses.length > 0;
  for (const child of container.children) {
    if (skipFirst && child.nodeName === 'P') {
      skipFirst = false;
      continue;
    }
    walk(child);
  }
  
  if (currentNum !== null && currentText.trim()) {
    const exists = verses.find(v => v.num === currentNum);
    if (!exists) verses.push({num: currentNum, text: currentText.trim()});
  }
  
  return verses;
})()

Step 3: Clean up verse text

In Python, clean each verse's text:

  • Remove cross-reference markers like (A), (B)re.sub(r'\s*\([A-Z]\)', '', text)
  • Strip trailing section heading artifacts
  • Handle smart quotes and special characters

Step 4: Format output

lines = []
for v in verses:
    lines.append(f"### {v['num']}")
    lines.append(f"> {v['text']}")
    lines.append("-")
    lines.append("")
output = "\n".join(lines).rstrip("\n")

Return formatted text inside a markdown code block.

Pitfalls

  • Verse 1: BibleGateway displays chapter number merged with verse 1 text. Handled specially.
  • Section headings: H3/H4 headings leak into verse text. The JS walker skips them.
  • Cross references: NIV has (A), (B) markers. Strip in cleanup.
  • Footnotes: May appear in last verse. Strip after "Footnotes" or cross-reference sections.
  • Rate limiting: BibleGateway may rate-limit. Retry after a few seconds.

Verification

Spot-check: verse count, first/last verses clean, no heading text in verse bodies, no footnote content.