I built a small web server with Sanic, this is one of the views:
@app.route("/query_video/", methods=["GET"])
async def video_query_views(request: Request):
keyword = request.args.get("keyword", None)
page = request.args.get("page", None)
order = request.args.get("order", None)
query_params = dict()
if keyword:
query_params['keyword'] = keyword
else:
return json("keyword can't be empty", status=403)
if page:
query_params['page'] = page
if order:
query_params['order'] = order
try:
rest = dict()
rest['bilibili'] = await get_bilibili_query(**query_params)
rest['qq'] = await get_v_qq_query(**query_params)
rest['youku'] = await get_youku_query(**query_params)
rest['iqiyi'] = await get_iqiyi_query(**query_params)
rest['mg'] = await get_mgtv_query(**query_params)
except Exception as e:
print(traceback.format_exc())
return json(str(e), status=403)
return json(rest)
And the error both occurred in get_bilibili_query, get_v_qq_query, ..., the code of function get_bilibili_query:
async def get_bilibili_query(keyword, page=1, order='tolalrank'):
base_url = "https://search.bilibili.com/all"
query_params = {
"keyword": keyword,
"page": page,
"order": order,
}
async with aiohttp.ClientSession() as session:
html = await fetch(session, url=base_url, params=query_params)
html = etree.HTML(html)
lis = html.xpath("//li[@class='video matrix ']")
rst = list()
for li in lis:
try:
a = li.xpath('a')[0]
img = a.xpath('div/img')[0]
time_len = a.xpath('div/span/text()')[0].strip()
title = a.xpath("@title")[0].strip()
origin_url = urljoin(base_url, a.xpath('@href')[0].strip())
img_url = urljoin(base_url, img.xpath("@data-src")[0].strip())
rst.append({
'origin_url': origin_url,
'time_len': time_len,
'img_url': img_url,
'title': title
})
except Exception:
continue
else:
return rst
function fetch:
async def fetch(session, url, **kwargs):
with async_timeout.timeout(10):
async with session.get(url, **kwargs, verify_ssl=False) as response:
return await response.text()
Just use aiohttp.ClientSession method [get]
The error detail:
Traceback (most recent call last):
File "/Users/angelo/PycharmProjects/pp3/s1.py", line 51, in video_query_views
rest['iqiyi'] = await get_iqiyi_query(**query_params)
File "/Users/angelo/PycharmProjects/pp3/sp.py", line 25, in get_bilibili_query
html = await fetch(session, url=base_url, params=query_params)
File "/Users/angelo/PycharmProjects/pp3/sp.py", line 13, in fetch
async with session.get(url, **kwargs, verify_ssl=False) as response:
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/client.py", line 690, in __aenter__
self._resp = yield from self._coro
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/client.py", line 267, in _request
conn = yield from self._connector.connect(req)
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 402, in connect
proto = yield from self._create_connection(req)
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 748, in _create_connection
_, proto = yield from self._create_direct_connection(req)
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 831, in _create_direct_connection
req=req, client_error=client_error)
File "/Users/angelo/PycharmProjects/env3.5/lib/python3.5/site-packages/aiohttp/connector.py", line 796, in _wrap_create_connection
return (yield from self._loop.create_connection(*args, **kwargs))
TypeError: 'coroutine' object is not iterable
But, if I run the function directly, no error occurred! Help please
It's a bug in uvloop, actually Cython: https://github.com/MagicStack/uvloop/issues/122
The same code with uvloop disabled works fine.