如何解决如何同时运行 selenium webdriver 方法和 requests 方法?
我需要同时运行两种方法。一种方法来自 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 举报,一经查实,本站将立刻删除。