Python如何将HTML5导出按钮创建的CSV读取到数据帧中?

2024-06-16 11:04:11 发布

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

我能够使用以下方法将带有关联URL的CSV文件直接读取到Python中:

import pandas as pd
URL = 'http://samplecsvs.s3.amazonaws.com/SalesJan2009.csv'
pd.read_csv(URL)

然而,我同样希望将HTML5导出按钮创建的CSV文件直接读取到Python3中(而不是在本地下载文件并将其上载到Python)

例如,我希望通过单击此网页上的“CSV”按钮创建的CSV文件作为数据帧直接读入Python: https://datatables.net/extensions/buttons/examples/html5/simple.htmlButton Location is shown here

我曾尝试在Python3中使用硒、BeautifulSoup和Pandas的组合,但没有成功


Tags: 文件csv方法importhttpurlpandass3
1条回答
网友
1楼 · 发布于 2024-06-16 11:04:11

单击按钮执行JavaScript,并且没有任何URL。但是您可以使用^{}函数来加载数据(它们在页面内以<table>形式)

例如:

import requests
import pandas as pd


url = 'https://datatables.net/extensions/buttons/examples/html5/simple.html'

df = pd.read_html(requests.get(url).text)[0]
print(df)

印刷品:

                   Name                       Position         Office  Age  Start date      Salary
0           Tiger Nixon               System Architect      Edinburgh   61  2011/04/25    $320,800
1       Garrett Winters                     Accountant          Tokyo   63  2011/07/25    $170,750
2            Ashton Cox        Junior Technical Author  San Francisco   66  2009/01/12     $86,000
3          Cedric Kelly    Senior Javascript Developer      Edinburgh   22  2012/03/29    $433,060
4            Airi Satou                     Accountant          Tokyo   33  2008/11/28    $162,700
5    Brielle Williamson         Integration Specialist       New York   61  2012/12/02    $372,000
6       Herrod Chandler                Sales Assistant  San Francisco   59  2012/08/06    $137,500
7        Rhona Davidson         Integration Specialist          Tokyo   55  2010/10/14    $327,900
8         Colleen Hurst           Javascript Developer  San Francisco   39  2009/09/15    $205,500
9           Sonya Frost              Software Engineer      Edinburgh   23  2008/12/13    $103,600

... and so on.

相关问题 更多 >