从只读html源写入csv文件

2024-04-26 04:02:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有麻烦导出一个html表(表2)到一个csv文件。我试着用它来监视这个物体pd.read\u html格式但不起作用

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import html.parser
import pandas as pd
import time

driver=webdriver.Chrome("C:/Users/Juan Diego Bernate V/Documents/Python/Practica/APIs/chromedriver.exe")

driver.get('https://www.ambito.com/contenidos/dolar-futuro.html')
res=driver.execute_script("return document.documentElement.outerHTML")
html=driver.page_source
soup= BeautifulSoup(html, 'lxml')
tabla=soup.find_all('table')
tabla2=pd.read_html(html)

dia = time.strftime("%Y%m%d")
ruta='C:/Users/Juan Diego Bernate V/'
nombre= ruta+ 'dolar_fut_rofex_2'+dia+'.csv'

tabla2.to_csv(nombre)
print (tabla2)
print (tabla)

这是错误信息,我写了所有我用的代码,以前,我认为这是不相关的。你知道吗

DevTools监听:ws://127.0.0.1:54234/DevTools/browser/6a47dd26-ab5d-4318-b858-226180882e0f 回溯(最近一次呼叫): “文件”WSDinamicoRofex.py文件,第34行,在 表2.至csv(标称) AttributeError:“list”对象没有“to\u csv”属性 希望你能帮助我


Tags: 文件csvfromimportreadtimehtmldriver
1条回答
网友
1楼 · 发布于 2024-04-26 04:02:51

我不能很好地运行您的示例,但请尝试^{}。这样做应该:

from astropy.io import ascii

# your code here

tbl = ascii.read(html_table, format='html')

这将生成一个astropy^{},非常容易操作,然后可以使用

ascii.write(tbl, 'table.csv', format='csv')

或者,等价地

tbl.write('table.csv', format='ascii.csv')

相关问题 更多 >