html python 交互_阅读与阅读使用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分钟(你可能需要在某些时候停止它)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值