利用三方库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