Python进阶之利用三方库aiohttp实现异步编程

利用三方库aiohttp实现异步编程

1、三方库aiohttp的使用方法

UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'


async def fetch_title(url):
    # session用完了是需要关上的,所以要利用上下文语法with
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers={
     'User-Agent': UA}, ssl=False) as resp:
            # ssl=False是为了不进行网站验证
            page_code = await resp.text()
            # 如果是拿二进制数据就是resp.read()
            match = TITLE_PATTERN.search(page_code)
            if match:
                print(match.group('foo'))

            # print(resp.status)
            # print(await resp.text())
            # await time.sleep(1)
            # # print()

url = [
    'https://pypi.org/project/aiohttp/',
    'https://www.baidu.com/',
]
# 正则表达式命名捕获组(具名捕获组)?P.*?
TITLE_PATTERN = re.compile(r'\(?P.*?)\<\/title\>')
url_list = [fetch_title(n) for n in url]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(url_list))
loop.close()

2、利用异步编程实现360图片下载

async def download_picture(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, ssl=False) as resp:
            data = await resp.read()  # 写await的条件是后面的那个函数是异步函数
            # # 字符串切片,切最后一个/后面的内容
            filename = url[url.rfind('/') + 1:]
            async with aiofile.async_open(f'images/{filename}', 'wb') as file:
                await file.write(data)


def main():
    # 第一步:拿到事件循环
    loop = asyncio.get_event_loop()
    for num in range(1, 6):
        resp = requests.get(f'https://image.so.com/zjl?ch=beauty&sn={num * 30}')
        data_dict = resp.json()
        url_list = []   # 为了放协程对象,
        for beauty_dict in data_dict['list']:
            picture_url = beauty_dict['qhimg_url']
            # Todo: Add your code here!!!
            # 第二步:调用异步函数,获得协程对象,放到列表中保存
            # url_list = [download_picture(n) for n in picture_url] 这样也可以
            url_list.append(download_picture(picture_url))
        # 第三步:把保存协程对象的列表变成一个Task对象注册到事件循环上
        loop.run_until_complete(asyncio.wait(url_list))
    # 第四步:关闭事件循环
    # loop.close()


if __name__ == '__main__':
    main()

你可能感兴趣的:(python初学,列表,python,正则表达式,java,json)