python - 在连续抓取模式下使用 pyppeteer
问题描述
每个示例和用例都使用 pyppeteer,其中浏览器会立即打开和关闭。例如从 pyppeteer 导入启动导入 asyncio
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('http://someurl')
content = await page.content()
cookieslist=await page.cookies()
cookiejar=createCookieJar(cookieslist)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
如果您想保持浏览器打开并不断抓取数据会怎样?这很容易用 selenium 完成,但是使用 pyppeteer,它似乎没有 asyncio 就无法工作。另一种使其工作的方法是保存会话并通过 schedule 和scrape重新打开浏览器,但这感觉是一种非常低效的方法。有人试过吗?
解决方案
您可以使用asyncio.Queue并不断地将数据泵入队列:
import asyncio
import traceback
from contextlib import suppress
from pyppeteer import launch
WORKERS = 10
URLS = [
"http://airbnb.com",
"http://amazon.co.uk",
"http://amazon.com",
"http://bing.com",
"http://djangoproject.com",
"http://envato.com",
"http://facebook.com",
"http://github.com",
"http://google.co.uk",
"http://google.com",
"http://google.es",
"http://google.fr",
"http://heroku.com",
"http://instagram.com",
"http://linkedin.com",
"http://live.com",
"http://netflix.com",
"http://rubyonrails.org",
"http://shopify.com",
"http://stackoverflow.com",
"http://trello.com",
"http://wordpress.com",
"http://yahoo.com",
"http://yandex.ru",
"http://yiiframework.com",
"http://youtube.com",
]
async def worker(q, browser):
# One tab per worker
page = await browser.newPage()
with suppress(asyncio.CancelledError):
while True:
url = await q.get()
try:
await page.goto(url, {"timeout": 10000})
html = await page.content()
except Exception:
traceback.print_exc()
else:
print(f"{url}: {len(html)}")
finally:
q.task_done()
await page.close()
async def main():
q = asyncio.Queue()
browser = await launch(headless=True, args=["--no-sandbox"])
tasks = []
for _ in range(WORKERS):
tasks.append(asyncio.create_task(worker(q, browser)))
for url in URLS:
await q.put(url)
await q.join()
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
推荐阅读
- firebase - 如何删除新的 Firebase 实时数据库?
- unity3d - Unity Unet 从玩家动作调整同步变量?- 我究竟做错了什么?
- python - Python覆盖最后一行而不附加
- odata - OData(大)元数据不起作用
- c - 如何从文件中逐行读取
- android - dlib问题的Android人脸识别
- mongodb - db.collection.insert({name:"Xyz"}) 和 db.MyDatabase.insert({name:"Xyz"}) 之间的区别
- ssh - gitlab.com SSH 连接超时
- java - 从 URL 中提取数据以用于 selenium 测试
- mesh - 如何撤消 gmsh 中的操作