{"id":89,"date":"2026-05-20T10:36:17","date_gmt":"2026-05-20T02:36:17","guid":{"rendered":"https:\/\/www.devfm.cn\/?p=89"},"modified":"2026-05-20T10:36:17","modified_gmt":"2026-05-20T02:36:17","slug":"downloader","status":"publish","type":"post","link":"https:\/\/www.devfm.cn\/?p=89","title":{"rendered":"downloader"},"content":{"rendered":"<pre><code class=\"language-python line-numbers\">import os\nimport asyncio\nimport aiohttp\nimport aiofiles\n\nsemaphore = asyncio.Semaphore(10)\n\nasync def get_file(url, dir, file_name=None, file_suf=None, session=None):\n    if session is None:\n        session = aiohttp.ClientSession()\n    if file_name is None:\n        file_name = url.split(\"\/\")[-1].split(\".\")[0:-1]\n    if file_suf is None:\n        file_suf = url.split(\"\/\")[-1].split(\".\")[-1]\n    try:\n        headers = {\n            \"User-Agent\": \"Mozilla\/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) \",\n            \"Accept\": \"text\/html,application\/xhtml+xml,application\/xml;q=0.9,*\/*;q=0.8\",\n            \"Accept-Language\": \"en-us\",\n            \"Connection\": \"keep-alive\",\n            \"Accept-Charset\": \"GB2312,utf-8;q=0.7,*;q=0.7\"}\n        file_path = r'{}\/{}.{}'.format(dir, file_name, file_suf)\n        if os.path.exists(file_path):\n            pass\n        async with semaphore:\n            async with session.get(url, headers=headers) as res:\n                res = await res.content.read()\n                async with aiofiles.open(file_path, 'ab') as f1:\n                    await f1.write(res)\n                    await f1.flush()\n                    print('\u4e0b\u8f7d\u5b8c\u6210\uff1a{}'.format(url))\n    except TypeError:\n        print('\u6587\u4ef6\u94fe\u63a5\u4e3a\u7a7a\uff0c\u8df3\u8fc7\uff01')\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import asyncio import aiohttp import aiofiles&hellip; <a class=\"read-more-link\" href=\"https:\/\/www.devfm.cn\/?p=89\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":1,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}