Files
python/python爬虫/crawl4ai.ipynb
T

68 lines
3.7 KiB
Plaintext
Raw Normal View History

2025-08-05 09:19:34 +08:00
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Crawl4AI",
"id": "15b6b10304dc52ed"
},
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-07-10T01:25:20.945463Z",
"start_time": "2025-07-10T01:25:18.137803Z"
}
},
"source": [
"import asyncio\n",
"from crawl4ai import AsyncWebCrawler\n",
"async def main():\n",
" async with AsyncWebCrawler() as crawler:\n",
" result = await crawler.arun(\"https://example.com\")\n",
" print(result.markdown[:300]) # 打印前 300 个字符\n",
"if __name__ == \"__main__\":\n",
" asyncio.run(main())"
],
"outputs": [
{
"ename": "RuntimeError",
"evalue": "asyncio.run() cannot be called from a running event loop",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mRuntimeError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[1], line 8\u001B[0m\n\u001B[0;32m 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(result\u001B[38;5;241m.\u001B[39mmarkdown[:\u001B[38;5;241m300\u001B[39m]) \u001B[38;5;66;03m# 打印前 300 个字符\u001B[39;00m\n\u001B[0;32m 7\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;18m__name__\u001B[39m \u001B[38;5;241m==\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m__main__\u001B[39m\u001B[38;5;124m\"\u001B[39m:\n\u001B[1;32m----> 8\u001B[0m asyncio\u001B[38;5;241m.\u001B[39mrun(main())\n",
"File \u001B[1;32mD:\\ProgramTools\\Anaconda\\Lib\\asyncio\\runners.py:190\u001B[0m, in \u001B[0;36mrun\u001B[1;34m(main, debug, loop_factory)\u001B[0m\n\u001B[0;32m 161\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Execute the coroutine and return the result.\u001B[39;00m\n\u001B[0;32m 162\u001B[0m \n\u001B[0;32m 163\u001B[0m \u001B[38;5;124;03mThis function runs the passed coroutine, taking care of\u001B[39;00m\n\u001B[1;32m (...)\u001B[0m\n\u001B[0;32m 186\u001B[0m \u001B[38;5;124;03m asyncio.run(main())\u001B[39;00m\n\u001B[0;32m 187\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m 188\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m events\u001B[38;5;241m.\u001B[39m_get_running_loop() \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m 189\u001B[0m \u001B[38;5;66;03m# fail fast with short traceback\u001B[39;00m\n\u001B[1;32m--> 190\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mRuntimeError\u001B[39;00m(\n\u001B[0;32m 191\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124masyncio.run() cannot be called from a running event loop\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m 193\u001B[0m \u001B[38;5;28;01mwith\u001B[39;00m Runner(debug\u001B[38;5;241m=\u001B[39mdebug, loop_factory\u001B[38;5;241m=\u001B[39mloop_factory) \u001B[38;5;28;01mas\u001B[39;00m runner:\n\u001B[0;32m 194\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m runner\u001B[38;5;241m.\u001B[39mrun(main)\n",
"\u001B[1;31mRuntimeError\u001B[0m: asyncio.run() cannot be called from a running event loop"
]
}
],
"execution_count": 1
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}