在用 sanic 做一個小項目的時候, 碰到上面這個問題。這是其中一個 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)
其中get_bilibili_query, get_v_qq_query之類的都是一個aiohttp 寫小爬蟲,具體:
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("http://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
報錯在 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()
具體錯誤如下:
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
感覺錯誤 報的很奇怪,當我直接用 aiohttp 的 web 執(zhí)行的時候,是沒有問題的,直接 loop 執(zhí)行也沒有問題。只有 sanic 執(zhí)行的時候會報錯,但報錯是在aiohttp 中報的。
loop = asyncio.get_event_loop()
loop.run_until_complete(get_bilibili_query(keyword='python'))
這么執(zhí)行時不會報錯。
求助。。。。
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數據專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯(lián)網管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網頁制作和網頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。