break response.read().decode('gbk')
break response.read().decode('gbk')
^
SyntaxError: invalid syntax
这是什么 import?这是美汤例子:
from bs4 import BeautifulSoup
import aiohttp
import asyncio
def grab_title(body): # 网页题目
soup = BeautifulSoup(body, "lxml")
return soup.find('title')
async def get_response(client, url): # 网站回复
async with client.get(url) as resp:
return await resp.read()
async def get_site_content(): # 网页内容
async with aiohttp.ClientSession() as session:
百度 = f"http://www.baiduo.com/"
任务_1 = asyncio.ensure_future(get_response(session, 百度))
游戏 = f"http://www.4399.com/"
任务_2 = asyncio.create_task(get_response(session, 游戏))
百度内容 = await 任务_1 # wait till task 1 is complete
游戏内容 = await 任务_2 # wait till task 2 is complete
百度题目 = grab_title(百度内容)
游戏题目 = grab_title(游戏内容)
print('百度网站: ', 百度题目)
print('游戏网站: ', 游戏题目)
asyncio.get_event_loop().run_until_complete(get_site_content())