Selenium chromedriver 错误:不允许启动 AudioContext。必须在页面上的用户手势后恢复(或创建)

发布于 2025-01-21 03:10:49 字数 2349 浏览 0 评论 0 原文

我正在尝试访问此网站上的数据: https://vemcount.app/embed/embed/widget/widget/widget/uocrulpangwo5ft?localecale

到目前为止,我的代码如下:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException


def configure_driver():
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--use-fake-ui-for-media-stream")
    driver = webdriver.Chrome(executable_path="C:\\Users\\uqiabde1\\Downloads\\chromedriver.exe", options = chrome_options)
    return driver


def getNumber(driver):
    # Step 1: Go to website
    driver.get(f"https://vemcount.app/embed/widget/uOCRuLPangWo5fT?locale=en")
    # wait for the element to load
    try:
        WebDriverWait(driver, 10).until(lambda s: s.find_element_by_id("flex items-center").is_displayed())
    except TimeoutException:
        print("TimeoutException: Element not found")
        return None

    # Step 2: Create a parse tree of page sources after searching
    soup = BeautifulSoup(driver.page_source, "lxml")
    # Step 3: Iterate over the search result and fetch the number
    for number in soup.select("div.items-center"):
        number_needed = "p span"
        print({
            "title": number.select_one(number_needed).text,
        })

# create the driver object.
driver = configure_driver()
getNumber(driver)
# close the driver.
driver.close()

我在Chromedriver中收到以下错误

[0414/150051.086:信息:console(2)]“不允许启动AudioContext。必须在页面上的用户手势后恢复(或创建)。https:// goo [dot] gl/gl/gl/ 7k7wlu”,来源:(2)

我不确定要使用哪种chrome_option绕过此错误。我尝试了一些,例如

--no-user-gesture-required

--disable-gesture-requirement-for-presentation

您的帮助将不胜感激。 谢谢。

I am trying to access data on this website:
https://vemcount.app/embed/widget/uOCRuLPangWo5fT?locale=en

My code so far is as follows:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException


def configure_driver():
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--use-fake-ui-for-media-stream")
    driver = webdriver.Chrome(executable_path="C:\\Users\\uqiabde1\\Downloads\\chromedriver.exe", options = chrome_options)
    return driver


def getNumber(driver):
    # Step 1: Go to website
    driver.get(f"https://vemcount.app/embed/widget/uOCRuLPangWo5fT?locale=en")
    # wait for the element to load
    try:
        WebDriverWait(driver, 10).until(lambda s: s.find_element_by_id("flex items-center").is_displayed())
    except TimeoutException:
        print("TimeoutException: Element not found")
        return None

    # Step 2: Create a parse tree of page sources after searching
    soup = BeautifulSoup(driver.page_source, "lxml")
    # Step 3: Iterate over the search result and fetch the number
    for number in soup.select("div.items-center"):
        number_needed = "p span"
        print({
            "title": number.select_one(number_needed).text,
        })

# create the driver object.
driver = configure_driver()
getNumber(driver)
# close the driver.
driver.close()

I get the following error in chromedriver

[0414/150051.086:INFO:CONSOLE(2)] "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo[dot]gl/7K7WLu", source: https://vemcount.app/build/embed.js?id=2ff0173dd78c5c1f99c6 (2)

I am not sure which chrome_option to use to bypass this error. I tried a few such as

--no-user-gesture-required

and

--disable-gesture-requirement-for-presentation

Your help would be highly appreciated.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2025-01-28 03:10:49

如果您有相同的查询或在动态网络刮擦方面正在努力,我找到了一种无需使用 selenium bs4 而刮擦数据的方法。

我使用的剧作家更加简单,并且非常感谢 innion_html()函数,该功能直接读取到动态FLEX HTML代码中。这是供参考的代码。

#part of the help to write the script I got from https://stackoverflow.com/questions/64303326/using-playwright-for-python-how-do-i-select-or-find-an-element

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(slow_mo=1000)

    page = browser.new_page()
    page.goto('https://vemcount.app/embed/widget/uOCRuLPangWo5fT')
    central = page.query_selector("p.w-full span");
    print({'central': central.inner_html()})
        
    browser.close()
 

如果有一种更好的方法,我很乐意听到您的建议。

您的,

Python的初学者。 :)

Just in case you had the same query or were struggling with dynamic web scraping I found a way to scrape the data without using selenium and bs4.

I used playwright which is far more straightforward and has a very much appreciated inner_html() function which reads straight into the dynamic flex HTML code. Here is the code for reference.

#part of the help to write the script I got from https://stackoverflow.com/questions/64303326/using-playwright-for-python-how-do-i-select-or-find-an-element

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(slow_mo=1000)

    page = browser.new_page()
    page.goto('https://vemcount.app/embed/widget/uOCRuLPangWo5fT')
    central = page.query_selector("p.w-full span");
    print({'central': central.inner_html()})
        
    browser.close()
 

If there is a better way I am more than happy to hear your suggestions.

Yours,

A Beginner in Python. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文