问题描述
我需要同时运行两种方法。一种方法来自 selenium(打开浏览器 -> 查找元素 -> 将文本附加到一个数组 -> 返回一个数组),第二个(AsyncHTMLSession)发送 GET 来检索 json。两种方法都可以完成它们的工作,但不能同时工作 :( 我使用了 requests-html AsyncHTMLSession
,它使用了 asyncio
硒:
async def get_matches(self):
matches_data = []
if first := await self._find_element(20,"visible","css","div[class='br-radius-xs sport sport-opened sport-bgr-soccer'] div[class='p-rel'] a[class='home shorten-label']"):
first.click()
matches_data.append((await self._get_time(),await self._get_name()))
others = await self._find_element(10,"ul[class='w-games bg-white br br-l-md sport-br-c-futbal'] div[class='w-game-opponents shorten-label']","many")
for other in others:
self.execute_script("""
const element = arguments[0]
element.click()
""",other)
matches_data.append((await self._get_time(),await self._get_name()))
return(matches_data)
请求:
async def beltic(self):
"""
Returns information from the "mother" portal to get compared with others
:return:
"""
belticLinks = filter(lambda x: (x["competition"]["sport"]["name"] == "Piłka nożna"),await self.settingsInstance._raw_json(self.data.betclic["link"]))
for link in belticLinks:
print (link["name"],timedelta(seconds=link["liveData"]["scoreboard"]["elapsedtime"]))
请求的基类:
class Settings(AsyncHTMLSession):
def __init__(self):
AsyncHTMLSession.__init__(self)
async def _body(self,link: str):
"""
:param link:
:return response with html:
"""
results = await self.get(link)
return results
async def _raw_json(self,link: str):
"""
:param link:
:return response with json:
"""
body = await self.get(link)
return body.json()
def _task_creator(self,func,lista: list,*args):
"""
Creates and stores async tasks in list-type variable
:param func:
:param lista:
:param args:
:return:
"""
async def create_task():
await func(*args)
lista.append(create_task)
任务触发:
self.settingsInstance._task_creator(self.beltic,self.tasks)
self.settingsInstance._task_creator(self.seleniumScraper.get_matches,self.tasks)
self.settingsInstance._runner(self.tasks)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)