{"id":129,"date":"2020-12-21T17:08:52","date_gmt":"2020-12-21T08:08:52","guid":{"rendered":"http:\/\/localhost:8000\/?p=129"},"modified":"2021-01-16T14:14:02","modified_gmt":"2021-01-16T05:14:02","slug":"pyppeteer-fire-download","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/12\/pyppeteer-fire-download.html","title":{"rendered":"pyppeteer\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9"},"content":{"rendered":"

\u6628\u65e5\u3001puppeteer\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/a> \u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001puppeteer\u306epython\u306b\u79fb\u690d\u3057\u305f pyppeteer<\/a> \u3067\u540c\u3058\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

pyppeteer\u81ea\u4f53\u306e\u4f7f\u3044\u65b9\u306f\u4ee5\u524d\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002
\n
pyppeteer\u306e\u4f7f\u3044\u65b9<\/a><\/p>\n

\u5b9f\u88c5<\/h2>\n

puppeteer\u3067\u8a66\u3057\u305f\u4e2d\u3067\u4e00\u756a\u7b4b\u304c\u826f\u3055\u305d\u3046\u3060\u3063\u305f\u300c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306erequest\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3066\u3001\u540c\u3058\u5185\u5bb9\u3067\u5225\u9014request\u3092\u9001\u4fe1\u3059\u308b\u300d\u3068\u3044\u3046\u65b9\u6cd5\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

from typing import Union, List, Dict\nimport asyncio\nfrom uuid import uuid4\nfrom mimetypes import guess_extension\n\nfrom pyppeteer.launcher import launch\nfrom pyppeteer.page import Page, Request\nfrom pyppeteer.element_handle import ElementHandle\nfrom pyppeteer.browser import Browser\n\nimport requests\nfrom requests import Response\n\ndef main():\n    asyncio.get_event_loop().run_until_complete(\n        download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".csv"]'))\n    asyncio.get_event_loop().run_until_complete(\n        download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".pdf"]'))\n    asyncio.get_event_loop().run_until_complete(\n        download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-pdf'))\n    asyncio.get_event_loop().run_until_complete(\n        download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-xml'))\n\nasync def download(target_url: str, selector: str):\n    browser: Browser = await launch(headless=True)\n    try:\n        page: Page = await browser.newPage()\n        await asyncio.gather(page.goto(target_url), page.waitForNavigation())  # \u5bfe\u8c61\u30da\u30fc\u30b8\u306b\u79fb\u52d5\n        cookies: List[Dict[str, Union[str, int, bool]]] = await page.cookies()  # cookie\u3092\u53d6\u5f97\n\n        await page.setRequestInterception(True)  # \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30c8\n        page.on('request',\n                lambda request: asyncio.create_task(send_request(request, cookies)))  # \u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u5185\u5bb9\u3067\u5225\u9014\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n\n        link: ElementHandle = await page.querySelector(selector)\n        await link.click()  # \u5bfe\u8c61\u30ea\u30f3\u30af\u3092\u30af\u30ea\u30c3\u30af\n\n    finally:\n        await browser.close()\n\nasync def send_request(request: Request, cookies: List[Dict[str, Union[str, int, bool]]]):\n    response: Response = requests.request(\n        url=request.url,\n        method=request.method,\n        headers=request.headers,\n        cookies=dict(map(lambda cookie: (cookie['name'], cookie['value']), cookies)),\n        data=request.postData,\n    )\n    extension: str = guess_extension(response.headers.get("content-type"))\n    output_path: str = f'downloads\/{uuid4()}{extension}'\n    with open(output_path, mode='wb') as file:\n        file.write(response.content)\n\nif __name__ == "__main__":\n    main()<\/code><\/pre>\n

\u5b9f\u969b\u306b4\u3064\u306e\u30da\u30fc\u30b8\u3067pdf\u3092\u4e8c\u3064\u3001xml\u3001csv\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u307f\u305f\u306e\u3067\u3059\u304c\u3001\u3042\u3063\u3055\u308a\u52d5\u3044\u3066\u304f\u308c\u307e\u3057\u305f\u3002<\/p>\n

\u7d30\u304b\u3044\u90e8\u5206\u3067\u591a\u5c11\u306e\u5de5\u592b\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u4e3b\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n