如何用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
相关产品推荐
相关产品推荐

