crawlbaseДокументация
Войти

Использование API

Добавьте &scraper=stackexchange-thread к запросу Crawling API. URL-кодируйте целевой URL в параметре url.

curl 'https://api.crawlbase.com/?token=YOUR_TOKEN' \
  --data-urlencode 'url=https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file' \
  --data-urlencode 'scraper=stackexchange-thread' -G
from crawlbase import CrawlingAPI

api = CrawlingAPI({'token': 'YOUR_TOKEN'})
res = api.get(
    'https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file',
    {'scraper': 'stackexchange-thread'}
)

import json
data = json.loads(res['body'])
const { CrawlingAPI } = require('crawlbase');
const api = new CrawlingAPI({ token: 'YOUR_TOKEN' });

const res = await api.get(
  'https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file',
  { scraper: 'stackexchange-thread' }
);
const data = JSON.parse(res.body);
require 'crawlbase'
api = Crawlbase::API.new(token: 'YOUR_TOKEN')

res = api.get('https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file', scraper: 'stackexchange-thread')
data = JSON.parse(res.body)

Пример входного URL

В параметре url работает URL любого вопроса Stack Exchange с любого сайта сети. Например:

https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file
https://superuser.com/questions/441895/automate-opening-html-and-printing-to-pdf
https://askubuntu.com/questions/1206658/why-is-apt-held-back
https://serverfault.com/questions/439471/which-openvpn-cipher-should-i-use
https://mathoverflow.net/questions/44265/is-the-riemann-hypothesis

Структура ответа

Тело ответа в формате JSON. Типы полей могут быть null, если исходная страница не содержит значения.

question
object
Сам вопрос.
question.id
string
Идентификатор вопроса Stack Exchange.
question.title
string
Заголовок вопроса.
question.url
string
Канонический URL вопроса.
question.body
string
Текст тела вопроса.
question.score
integer
Счёт вопроса (плюсы минус минусы).
question.viewCount
integer
Количество просмотров вопроса.
question.tags
array
Теги, применённые к вопросу.
question.askedAt
string
Время создания вопроса (ISO 8601).
question.author
object
Автор вопроса.
question.author.name
string
Отображаемое имя автора вопроса.
question.author.url
string
URL профиля автора вопроса.
question.author.reputation
integer
Счёт репутации автора вопроса.
question.comments
array
Комментарии к вопросу.
question.comments[].id
string
Идентификатор комментария.
question.comments[].score
integer
Счёт комментария.
question.comments[].body
string
Текст комментария.
question.comments[].author
string
Отображаемое имя автора комментария.
question.comments[].authorUrl
string
URL профиля автора комментария.
question.comments[].createdAt
string
Время создания комментария (ISO 8601).
answerCount
integer
Количество ответов, возвращённых в answers.
answers
array
Ответы на вопрос, в порядке списка.
answers[].id
string
Идентификатор ответа.
answers[].score
integer
Счёт ответа (плюсы минус минусы).
answers[].isAccepted
boolean
True, если это принятый ответ.
answers[].body
string
Текст тела ответа.
answers[].author
object
Автор ответа.
answers[].author.name
string
Отображаемое имя автора ответа.
answers[].author.url
string
URL профиля автора ответа.
answers[].author.reputation
integer
Счёт репутации автора ответа.
answers[].createdAt
string
Время создания ответа (ISO 8601).
answers[].comments
array
Комментарии к ответу.
answers[].comments[].id
string
Идентификатор комментария.
answers[].comments[].score
integer
Счёт комментария.
answers[].comments[].body
string
Текст комментария.
answers[].comments[].author
string
Отображаемое имя автора комментария.
answers[].comments[].authorUrl
string
URL профиля автора комментария.
answers[].comments[].createdAt
string
Время создания комментария (ISO 8601).

Пример ответа

{
  "question": {
    "id": "2861071",
    "title": "How to modify a text file?",
    "url": "https://stackoverflow.com/questions/2861071/how-to-modify-a-text-file",
    "body": "I'm using Python and I need to insert a line at the start of a text file without loading the whole file into memory. Is there a way to do this in place, or do I have to rewrite the file?",
    "score": 312,
    "viewCount": 486201,
    "tags": ["python", "file", "text-files"],
    "askedAt": "2010-05-18T20:11:33Z",
    "author": {
      "name": "Nathan Fellman",
      "url": "https://stackoverflow.com/users/8460/nathan-fellman",
      "reputation": 127843
    },
    "comments": [
      {
        "id": "2954120",
        "score": 3,
        "body": "Do you need to preserve the original file, or is rewriting acceptable?",
        "author": "Greg Hewgill",
        "authorUrl": "https://stackoverflow.com/users/893/greg-hewgill",
        "createdAt": "2010-05-18T20:19:04Z"
      }
    ]
  },
  "answerCount": 2,
  "answers": [
    {
      "id": "2861108",
      "score": 401,
      "isAccepted": true,
      "body": "You cannot insert into the middle of a file without rewriting it. Read the file into a list of lines, insert your new line, then write it all back:\n\n    with open('file.txt') as f:\n        lines = f.readlines()\n    lines.insert(0, 'new first line\\n')\n    with open('file.txt', 'w') as f:\n        f.writelines(lines)\n",
      "author": {
        "name": "Roberto Bonvallet",
        "url": "https://stackoverflow.com/users/193568/roberto-bonvallet",
        "reputation": 30215
      },
      "createdAt": "2010-05-18T20:15:52Z",
      "comments": [
        {
          "id": "2954260",
          "score": 12,
          "body": "For very large files, stream through a temporary file instead of holding everything in memory.",
          "author": "John Machin",
          "authorUrl": "https://stackoverflow.com/users/253537/john-machin",
          "createdAt": "2010-05-18T21:02:11Z"
        }
      ]
    },
    {
      "id": "2861180",
      "score": 47,
      "isAccepted": false,
      "body": "If the file is large, use fileinput with inplace=True to edit it line by line without loading it all at once.",
      "author": {
        "name": "codeape",
        "url": "https://stackoverflow.com/users/18770/codeape",
        "reputation": 98412
      },
      "createdAt": "2010-05-18T20:24:39Z",
      "comments": []
    }
  ]
}