{"id":207577,"date":"2020-11-26T18:02:43","date_gmt":"2020-11-26T10:02:43","guid":{"rendered":"http:\/\/4563.org\/?p=207577"},"modified":"2020-11-26T18:02:43","modified_gmt":"2020-11-26T10:02:43","slug":"%e8%af%b7%e9%97%ae%e8%bf%99%e4%b8%aa-asyncio-%e5%bc%82%e6%ad%a5%e8%ae%bf%e9%97%ae%e9%a1%b5%e9%9d%a2%e6%80%8e%e4%b9%88%e5%86%99%e5%8f%af%e4%bb%a5%e6%9b%b4%e5%8a%a0%e5%bf%ab%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=207577","title":{"rendered":"\u8bf7\u95ee\u8fd9\u4e2a asyncio \u5f02\u6b65\u8bbf\u95ee\u9875\u9762\u600e\u4e48\u5199\u53ef\u4ee5\u66f4\u52a0\u5feb\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u95ee\u8fd9\u4e2a asyncio \u5f02\u6b65\u8bbf\u95ee\u9875\u9762\u600e\u4e48\u5199\u53ef\u4ee5\u66f4\u52a0\u5feb\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yagamil <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a \u5148\u5728\u4e3b\u9875\u83b7\u53d6\u5b50\u9875\u9762\uff0c\u5927\u6982 64 \u4e2a\uff0c\u7136\u540e\u5e76\u53d1\u8bbf\u95ee\u5b50\u9875\u9762\uff0c \u603b\u7528\u65f6\u5927\u6982 36s\uff0c\u611f\u89c9\u8fd8\u662f\u6709\u70b9\u6162\u3002<\/p>\n<pre><code> URL_MAP = {'home_page': 'https:\/\/xxx\/stocks\/industry', 'base': 'https:\/\/xxx.com'}   class App(BaseService):      def __init__(self):         super(App, self).__init__()       async def home_page(self):         start = time.time()         async with aiohttp.ClientSession() as session:              async with session.get(url=URL_MAP['home_page'], headers=headers) as response:                     html = await response.text()  # \u8fd9\u4e2a\u963b\u585e                     resp = Selector(text=html)                     industries = resp.xpath('\/\/ul[@class=\"list-unstyled\"]\/a')                     task_list =[]                     for industry in industries:                         json_data = {}                         industry_url = industry.xpath('.\/\/@href').extract_first()                         industry_name = industry.xpath('.\/\/li\/text()').extract_first()                         json_data['industry_url'] = industry_url                         json_data['industry_name'] = industry_name                          task = asyncio.ensure_future(self.detail_list(session, industry_url, json_data))                         task_list.append(task)                      await asyncio.gather(*task_list)                     end = time.time()                      print(f'time used {end-start}')      async def detail_list(self, session, url, json_data):          async with session.get(URL_MAP['base']+url, headers=headers) as response:             response = await response.text()             self.parse_detail(response, json_data)      def parse_detail(self, html, json_data=None):         resp = Selector(text=html)         # info = resp.xpath('\/\/div[@id=\"v_desc\"]\/div[@class=\"info open\"]\/text()').extract_first()         title =resp.xpath('\/\/title\/text()').extract_first()         print(title)   app = Holdle() loop = asyncio.get_event_loop() loop.run_until_complete(app.home_page()) <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4329593\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a\u6bcf\u4e2a\u8bf7\u6c42\u7528\u65f6\u6253\u51fa\u6765\u6328\u4e2a\u770b\u770b, \u627e\u4e0d\u5230\u518d\u8003\u8651\u4ee3\u7801\u5c42\u9762\u4f18\u5316(\u6bd4\u5982 pysnooper), \u770b\u8d77\u6765\u6ca1\u6709\u5f02\u5e38\u540c\u6b65\u4ee3\u7801\u963b\u585e\u534f\u7a0b\u7684\u5730\u65b9, \u8be5\u590d\u7528 Session \u7684\u5730\u65b9\u4e5f\u590d\u7528\u4e86, \u6240\u4ee5\u611f\u89c9\u4e3b\u8981\u7f51\u901f\u95ee\u9898\u4e0a&#8230;<\/p>\n<p>\u5982\u679c\u540c\u6b65\u4ee3\u7801\u8fd8\u60f3\u7a0d\u5fae\u63d0\u901f, \u53ef\u4ee5\u8bd5\u8bd5 selectolax, \u6d4b\u8fc7\u5b83\u548c lxml \u7684 cssselector \u6bd4\u8f83, \u5feb\u4e86\u5927\u7ea6\u4e00\u500d, \u4e0d\u8fc7\u6ca1\u505a xpath \u7684\u6bd4\u8f83, \u7528\u60ef\u4e86 css \u7684\u8bed\u4e49\u4ee5\u540e\u5f88\u5c11\u78b0 xpath \u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329594\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yagamil <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u55ef\uff0c\u5e94\u8be5\u662f\u7f51\u901f\u95ee\u9898\uff0c\u8f6c\u4e3a\u540c\u6b65\u540e\uff0c\u7528\u65f6\u4e3a 131s\uff0c\u662f\u5f02\u6b65\u7684 4 \u500d\u591a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329595\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : inframe <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             httpx \u4e5f\u53ef\u4ee5\uff0c\u6bd4 aiohttp \u8981\u66f4\u63a5\u8fd1 requests \u7684\u7528\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329596\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yagamil \u4e3b\u6d41 HTTP Client \u91cc\u9762, \u5df2\u7ecf\u57fa\u672c\u6ca1\u6709\u6bd4 aiohttp \u5feb\u7684\u5e93\u4e86&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329597\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Sparetire <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bb3\uff0c\u722c\u90a3\u4e48\u5feb\u505a\u5565\u5462\uff0c\u7ed9\u4eba\u53d1\u73b0\u4e86\u8fd8\u5f97\u6539\u4ee3\u7801\u3002\u3002\u5f53\u7136\u8981\u662f\u4e0d\u8bbe\u9632\u7684\u7ad9\u5f53\u6211\u6ca1\u8bf4<br \/>\u8bb2\u9053\u7406\u90fd\u5f02\u6b65\u5e76\u53d1\u4e86\u5269\u4e0b\u80fd\u8f83\u5927\u63d0\u5347\u7684\u70b9\u5c31\u7f51\u7edc\u5e26\u5bbd\u548c\u9875\u9762\u89e3\u6790\u4e86\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329598\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lithbitren <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             aiohttp \u7684 io \u5df2\u7ecf\u8db3\u591f\u5feb\u4e86\uff0c\u5199\u8d77\u6765\u6bd4 requests \u7e41\u7410\u4e00\u70b9\u70b9\u4e0d\u8fc7\u53ef\u4ee5\u81ea\u5df1\u5c01\u8d77\u6765\uff0c\u5355\u673a\u8bbf\u95ee\u4e91\u670d\u52a1\u5668\u53ef\u4ee5\u8fbe\u5230\u6bcf\u79d2 9000 \u5de6\u53f3\u7684\u5165\u5305\uff0c\u5269\u4e0b\u7684\u74f6\u9888\u5e94\u8be5\u57fa\u672c\u5c31\u662f\u89e3\u6790\u548c\u5e26\u5bbd\u7684\u95ee\u9898\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4329599\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chaogg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a\u89e3\u6790\u653e\u5230\u5355\u72ec\u7684\u8fdb\u7a0b\u4e2d\u8bd5\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u95ee\u8fd9\u4e2a asyncio \u5f02\u6b65\u8bbf\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/207577"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207577"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/207577\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207577"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}