python+html语音人物交互_使用Python读取HTML表并与之交互
這個頁面非常動態(而且非常慢,至少在我這邊是這樣),包括JavaScript和多個異步請求來獲取數據。用requests來實現這一點并不容易,您可能需要通過selenium使用瀏覽器自動化。在
這里有一些東西讓你開始。注意Explicit Waits的用法:import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://quotes.freerealtime.com/dl/frt/M?IM=quotes&type=Time%26Sales&SA=quotes&symbol=IBM&qm_page=45750")
wait = WebDriverWait(driver, 400) # 400 seconds timeout
# wait for select element to be visible
time_select = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select[name=time]"))))
# select 9:30 and go
time_select.select_by_visible_text("09:30")
driver.execute_script("arguments[0].click();", driver.find_element_by_id("go"))
time.sleep(2)
while True:
# wait for the table to appear and load to pandas dataframe
table = wait.until(EC.presence_of_element_located((By.ID, "qmmt-time-and-sales-data-table")))
df = pd.read_html(table.get_attribute("outerHTML"))
print(df[0])
# wait for offset select to be visible and forward it 1 min
offset_select = Select(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "select[name=timeOffset]"))))
offset_select.select_by_value("1")
time.sleep(2)
# TODO: think of a break condition
請注意,這在我的機器上運行得非常非常慢,我不確定它在你的機器上運行得有多好,但它一直在無休止的循環中向前推進1分鐘(您可能需要在某個時刻停止它)。在
總結
以上是生活随笔為你收集整理的python+html语音人物交互_使用Python读取HTML表并与之交互的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JAVA】StringTokenize
- 下一篇: zookeeper工作原理、安装配置、工