我试图访问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
任何帮助将不胜感激
答案 0 :(得分:2)
您只需等待加载主要内容:
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']