You need to enable JavaScript to run this app. 如何用Selenium爬取动态更新HTML表格并生成新条目置顶的按日CSV
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium爬取动态更新HTML表格并生成新条目置顶的按日CSV

爬取需求
  • 从指定币类数据页面抓取币类数据表,按日期维度生成CSV格式的数据文件
  • 更新逻辑:页面出现新的币类数据时,需要将对应条目新增到现有数据文件的顶部,而不是追加在文件末尾
  • 期望的输出格式示例:
Coin,Pings,...Datetime

BTC,25,...07:17:05 03/18/21
当前代码进度

目前尚未完成全流程开发,初步尝试编写的代码如下:

from selenium import webdriver
import numpy as np
import pandas as pd

firefox = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
firefox.get('https://agile-cliffs-23967.herokuapp.com/binance/')

rows = len(firefox.find_elements_by_xpath("/html/body/div/section[2]/div/div/div/div/table/tr"))
columns = len(firefox.find_elements_by_xpath("/html/body/div/section[2]/div/div/div/div/table/tr[1]/th"))

df = pd.DataFrame(columns=['Coin','Pings','Net Vol BTC','Net Vol per','Recent Total Vol BTC', 'Recent Vol per', 'Recent Net Vol', 'Datetime'])

for r in range(1, rows+1):
    for c in range(1, columns+1): 
        value = firefox.find_element_by_xpath("/html/body/div/section[2]/div/div/div/div/table/tr["+str(r)+"]/th["+str(c)+"]").text
        print(value)
        
#         df.loc[i, ['Coin']] = 

内容的提问来源于stack exchange,提问作者Neil RT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 10:36:04