我正在嘗試訪問此網站上的資料: https ://vemcount.app/embed/widget/uOCRuLPangWo5fT?locale=en
到目前為止,我的代碼如下:
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:INFO:CONSOLE(2)] "AudioContext 不允許啟動。它必須在頁面上的用戶手勢后恢復(或創建)。https://goo[dot]gl/7K7WLu",來源:https ://vemcount.app/build/embed.js?id=2ff0173dd78c5c1f99c6 (2)
我不確定使用哪個 chrome_option 來繞過此錯誤。我嘗試了一些,例如
--no-user-gesture-required
和
--disable-gesture-requirement-for-presentation
您的幫助將不勝感激。謝謝。
uj5u.com熱心網友回復:
以防萬一您有相同的查詢或正在為動態網路抓取而苦苦掙扎,我找到了一種無需使用seleniumand即可抓取資料的方法bs4。
我使用了 playwright,它更直接,并且有一個非常受歡迎的inner_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 初學者。:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/461093.html
