微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何同时运行 selenium webdriver 方法和 requests 方法?

如何解决如何同时运行 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?