gpt4 book ai didi

python - Selenium 和 phantomjs 不打开 javascript 页面

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:02 25 4
gpt4 key购买 nike

我正在尝试访问包含在 javascript 渲染页面中的赔率数据,但 selenium 似乎不起作用。这是我的代码:

from selenium import webdriver

browser = webdriver.PhantomJS()
browser.implicitly_wait(10)
browser.get('http://justbet.co.ke/index.php?option=com_jbt3&task=matches&league=58&Itemid=1')
print browser.page_source

任何帮助将不胜感激

最佳答案

您只需要等待主要内容加载:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".razrada")))

使用 this page 完成工作代码作为目标:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("http://justbet.co.ke/index.php?option=com_justbet&league=58&Itemid=1")

# wait for the page to load
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".razrada")))

for row in driver.find_elements_by_css_selector(".razrada > tbody > tr"):
print([td.text.replace("\n", " ")
for td in row.find_elements_by_css_selector("table.options td.option")])

打印:

['Crystal Palace 2.95', 'X 3.20', 'Everton FC 2.40']

关于python - Selenium 和 phantomjs 不打开 javascript 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com