{"id":294,"date":"2024-11-10T10:39:47","date_gmt":"2024-11-10T02:39:47","guid":{"rendered":"\/?p=294"},"modified":"2024-12-03T19:47:16","modified_gmt":"2024-12-03T11:47:16","slug":"python%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"\/?p=294","title":{"rendered":"Python-\u722c\u866b\u9879\u76ee"},"content":{"rendered":"\n<p>\u5f02\u6b65API\u56fe\u7247\u83b7\u53d6<\/p>\n\n\n\n<pre class=\"wp-block-code has-luminous-vivid-orange-color has-text-color has-link-color wp-elements-95a2472d4e1ec7861e0eeea6794ad24c\"><code>import asyncio\nimport aiohttp\nimport re\nfrom PIL import Image\nfrom io import BytesIO\ncunt = 0\nuurl = \"https:\/\/www.dmoe.cc\/random.php?return=json\"\n#\u201chttps:\/\/api.likepoems.com\/img\/pc\/\u201d\ncs = \"https:\/\/api.likepoems.com\/img\/pc\/\"\n\nclass PH:\n\nasync def geto(ul):\n    async with aiohttp.ClientSession() as asy:\n        global cunt\n        cunt += 1\n        incunt = cunt\n        print(f\"{incunt}\u51c6\u5907\u4e2d\")\n        try:\n            async with await asy.get(ul) as re:\n                request = Image.open(BytesIO(await re.content.read()))\n                request.save(f'png1\/{incunt}.png')\n                print(f\"{incunt}\u4e0b\u5b8c\u4e86\")\n        except Exception as e:\n            print(f\"ERROR:{e}\")\n\n#\u5f02\u6b65\nasync def zhidao(ul):\n    #\u5f15\u7528aiohttp\u4e2d\u7684ClientSession\u7c7b\n    async with aiohttp.ClientSession() as asy:\n        try:\n            global cunt\n            cunt += 1\n            incunt = cunt\n            print(f\"{incunt}\u6b63\u5728\u8fde\u63a5\")\n            #\u8c03\u7528ClientSession\u7c7b\u4e2d\u7684get\u51fd\u6570\n            async with await asy.get(ul) as req:\n                print(f\"{incunt}\u8fde\u63a5\u6210\u529f\")\n                #\u83b7\u53d6\u6587\u672c\u5185\u5bb9\n                gethtml = await req.text()\n                #\u5bf9\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u6b63\u5219\u5904\u7406\u83b7\u53d6url\n                url = re.search(r'\"imgurl\":\"(.*?)\"',gethtml).group(1).replace('\\\\','')\n                async with await asy.get(url) as ph:\n                    print(f\"{incunt}\u6b63\u5728\u4e0b\u8f7d\")\n                    #\u5bf9\u7f51\u7ad9\u56fe\u7247\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u5e76\u8fdb\u884c\u50a8\u5b58\n                    #ph.content.read()\n                    Photo = Image.open(BytesIO(await ph.content.read()))\n                    Photo.save(f'png\/{incunt}.png')\n                    print(f\"{incunt}\u4e0b\u5b8c\u4e86\")\n\n        except Exception as e:\n            print(e)\n\n\n\n\n\nasync def main1():\n    take = &#91;zhidao(uurl) for i in range(20)]\n    await asyncio.gather(*take)\nasync def main2():\n    take = &#91;geto(cs) for i in range(20)]\n    await asyncio.gather(*take)\n\nif __name__ == '__main__':\n    # important\n    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())\n    asyncio.run(main2())\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65API\u56fe\u7247\u83b7\u53d6<\/p>\n","protected":false},"author":1,"featured_media":298,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[21],"class_list":["post-294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/294"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":362,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions\/362"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/media\/298"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}