【问题标题】:Get the data from the website to the python从网站获取数据到python
【发布时间】:2020-08-05 12:51:10
【问题描述】:

我需要从这个网站https://www.tsa.gov/coronavirus/passenger-throughput访问表格。

我需要将此表作为 pandas 数据框。

以前,我只处理已经是 xls 或 csv 的 URL,所以我不知道如何从普通网站获取表格。

请帮忙!

【问题讨论】:

标签: python pandas web url


【解决方案1】:

我将尝试提供起点。您可以查看official documentation 了解更多详情。

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

req = Request('https://www.tsa.gov/coronavirus/passenger-throughput',  headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req).read()

soup = BeautifulSoup(html, 'html.parser')
for row in soup.find_all('tr'):
    print(row)

输出如下所示:

 <tr><td><strong>Date</strong></td><td><strong>Total Traveler Throughput</strong></td><td><strong>Total Traveler Throughput<br/>(1 Year Ago - Same Weekday)</strong></td></tr>
 <tr><td>4/21/2020</td><td>92,859</td><td>2,227,475</td></tr>
 <tr><td>4/20/2020</td><td>99,344</td><td>2,594,171</td></tr>
 <tr><td>4/19/2020</td><td>105,382</td><td>2,356,802</td></tr>
 <tr><td>4/18/2020</td><td>97,236</td><td>1,988,205</td></tr>
 <tr><td>4/17/2020</td><td>106,385</td><td>2,457,133</td></tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2012-03-27
    • 2018-05-23
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多