根据我在previous question中找到的回复,我可以通过web从URL:https://www.nytimes.com/interactive/2021/world/covid-vaccinations-tracker.html
抓取Python中的表,但它只抓取部分,直到出现“Show all”行
我如何用Python获取隐藏在“全部显示”之外的完整表
以下是我正在使用的代码:
import pandas as pd
import requests
from bs4 import BeautifulSoup
#
vaccineDF = pd.read_html('https://www.nytimes.com/interactive/2021/world/covid-vaccinations-tracker.html')[0]
vaccineDF = vaccineDF.reset_index(drop=True)
print(vaccineDF.head(100))
输出仅获取15行(直到全部显示):
Unnamed: 0_level_0 Doses administered ... Unnamed: 8_level_0 Unnamed: 9_level_0
Unnamed: 0_level_1 Per 100 people ... Unnamed: 8_level_1 Unnamed: 9_level_1
0 World 11 ... NaN NaN
1 Israel 116 ... NaN NaN
2 Seychelles 116 ... NaN NaN
3 U.A.E. 99 ... NaN NaN
4 Chile 69 ... NaN NaN
5 Bahrain 66 ... NaN NaN
6 Bhutan 63 ... NaN NaN
7 U.K. 62 ... NaN NaN
8 United States 61 ... NaN NaN
9 San Marino 60 ... NaN NaN
10 Maldives 59 ... NaN NaN
11 Malta 55 ... NaN NaN
12 Monaco 53 ... NaN NaN
13 Hungary 45 ... NaN NaN
14 Serbia 44 ... NaN NaN
15 Show all Show all ... Show all Show all
您不能直接打印整个数据,因为单击
Show all
按钮后可以看到完整的数据。因此,从这个场景中,我们可以理解,首先我们必须创建一个on click()
事件来单击Show all
按钮,然后才能获取整个表我已经为
on click
事件使用了Selenium
库来按下Show all
按钮。对于这个特定场景,我使用了Selenium
的Firefox() Webdriver
从url
获取所有data
。请参考下面给出的代码获取给定COVID Dataset URL
的整个表:在获取整个数据之后,让我们看看
COVID Dataset URL
中有多少个表现在,让我们获取数据表:-
正如您所看到的,它没有在我们的数据集中显示
show all
。现在我们可以把这个Data Table
转换成DataFrame
。为了完成这个任务,我们必须将这个Data
存储为CSV
格式,我们可以重新加载它并将它存储在DataFrame
。其代码如下所述:因此,在将所有
Data
存储为csv
格式之后,让我们将数据转换为DataFrame
格式并打印整个数据:-从上面的输出可以看出,我们已经成功地获取了整个
data table
。希望这个解决方案能对您有所帮助相关问题 更多 >
编程相关推荐