Python client library for Diffbot APIs.
pip install git+https://github.com/diffbot/diffbot-python.gitOr, for local development:
pip install -e ".[dev]"Set your Diffbot API token in your environment or .env.
export DIFFBOT_API_TOKEN=<TOKEN>from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
data = db.extract("https://www.example.com")from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
for chunk in db.ask([{"role": "user", "content": "What's the capital of France?"}]):
print(chunk, end="")from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
for event in db.crawl("https://www.example.com", hops=1):
print(event)from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
results = db.dql('type:Organization name:"Diffbot"')from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
results = db.web_search("diffbot knowledge graph")
for r in results["search_results"]:
print(r["score"], r["title"], r["pageUrl"])
print(r["content"])from diffbot import Diffbot
db = Diffbot(token="YOUR_TOKEN")
result = db.entities("Apple CEO Tim Cook announced record quarterly earnings.")
for entity in result["entities"]:
print(entity["name"], entity.get("type"), entity.get("id"))
print("sentiment:", result.get("sentiment"))import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
data = await db.extract("https://www.example.com")
print(data)
asyncio.run(main())import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
async for chunk in db.ask([{"role": "user", "content": "What's the capital of France?"}]):
print(chunk, end="")
asyncio.run(main())import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
async for event in db.crawl("https://www.example.com", hops=1):
print(event)
asyncio.run(main())import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
results = await db.dql('type:Organization name:"Diffbot"')
print(results)
asyncio.run(main())import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
results = await db.web_search("diffbot knowledge graph")
for r in results["search_results"]:
print(r["score"], r["title"], r["pageUrl"])
print(r["content"])
asyncio.run(main())import asyncio
from diffbot import DiffbotAsync
async def main():
async with DiffbotAsync(token="YOUR_TOKEN") as db:
result = await db.entities("Apple CEO Tim Cook announced record quarterly earnings.")
for entity in result["entities"]:
print(entity["name"], entity.get("type"), entity.get("id"))
print("sentiment:", result.get("sentiment"))
asyncio.run(main())This library also includes a CLI.
export DIFFBOT_API_TOKEN=your-token-here
db extract https://www.example.com
db ask "What's the capital of France?"
db crawl https://www.example.com --hops 1
db crawl-list-jobs
db crawl-delete-job crawl-1234567890
db web-search "diffbot knowledge graph"
db web-search "diffbot knowledge graph" -n 5 -f json
db entities "Apple CEO Tim Cook announced record quarterly earnings."
db entities "Apple CEO Tim Cook announced record quarterly earnings." -f dqlRun the mock test suite:
python -m pytestRun live integration tests against the real API (requires a valid token):
DIFFBOT_TOKEN=your_token python -m pytest -m live