{"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 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 \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 \u6614\u8a66\u3057\u3066\u307f\u305f\u6642\u306f\u3084\u308a\u65b9\u304c\u5206\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u4eca\u56de\u306fpuppeteer\u3067\u3046\u307e\u304f\u3044\u304f\u65b9\u6cd5\u3092\u8a66\u3057\u3066\u307f\u305f\u5f8c\u306b\u3046\u307e\u304f\u3044\u3063\u305f\u65b9\u6cd5\u3092pyppeteer\u306b\u79fb\u690d\u3059\u308b\u3068\u3044\u3046\u5f62\u3092\u3068\u3063\u305f\u306e\u3067\u3001\u6bd4\u8f03\u7684\u7c21\u5358\u306b\u5b9f\u73fe\u3067\u304d\u305f\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u3061\u306a\u307f\u306b\u3001puppeteer\u3067\u306f\u3046\u307e\u304f\u52d5\u3044\u305f\u300c\u5f37\u5236\u7684\u306b\u30d6\u30e9\u30a6\u30b6\u306e\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u300d\u3068\u3044\u3046\u65b9\u3082\u9014\u4e2d\u307e\u3067\u5b9f\u88c5\u3057\u305f\u306e\u3067\u3059\u304c\u3001Chrome Devtools\u306e Fetch.enable<\/a> \u306erequestStage\u304c\u3069\u3046\u3057\u3066\u3082Response\u30b9\u30c6\u30fc\u30b8\u3067\u30a4\u30d9\u30f3\u30c8\u30ad\u30e3\u30d7\u30c1\u30e3\u3067\u304d\u306a\u304f\u3066\u8ae6\u3081\u307e\u3057\u305f\u3002\u3002<\/p>\n \u53c2\u8003\u307e\u3067\u3067\u3059\u304c\u3001\u4e0a\u8a18\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u3067\u516c\u958b\u3057\u3066\u3044\u307e\u3059\u3002 \u6628\u65e5\u3001puppeteer\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b \u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001puppeteer\u306epython\u306b\u79fb\u690d\u3057\u305f pyppeteer \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 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 pyppeteer\u306e\u4f7f\u3044\u65b9 \u5b9f\u88c5 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 from typing import Union, List, Dict import asyncio from uuid import uuid4 from mimetypes import guess_extension from pyppeteer.launcher import launch from py <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,7,9],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/129"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\npyppeteer\u306e\u4f7f\u3044\u65b9<\/a><\/p>\n\u5b9f\u88c5<\/h2>\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
\n
await page.setRequestInterception(True)<\/code>\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30c8\u53ef\u80fd\u306b\u3059\u308b<\/li>\n
page.on('request', callback)<\/code>\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3001\u305d\u306e\u4e2d\u3067\u3001requests\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u3001\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u306e\u3068\u540c\u3058\u5185\u5bb9\u3067\u5225\u9014\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b<\/li>\n<\/ul>\n
\u3055\u3044\u3054\u306b<\/h2>\n
\nhttps:\/\/github.com\/rinoguchi\/python_scraping_sample\/blob\/master\/src\/pyppeteer\/download_with_another_request.py<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"