Home > Enterprise >  wanted to extract table info from the webpage
wanted to extract table info from the webpage

Time:04-28

i just wanted to scrape the Countries in the world by population table from below table

"https://www.worldometers.info/world-population/population-by-country/"

sample data enter image description here

CodePudding user response:

You can grab the desired table data using pandas

import pandas as pd 
import requests

headers={"User-Agent":"mozilla/5.0"}
url='https://www.worldometers.info/world-population/population-by-country/'
red=requests.get(url,headers=headers).text

df = pd.read_html(red)[0]
print(df)

Output:

0      1                   China         1439323776  ...       38         61 %      18.47 
%
1      2                   India         1380004385  ...       28         35 %      17.70 
%
2      3           United States          331002651  ...       38         83 %       4.25 
%
3      4               Indonesia          273523615  ...       30         56 %       3.51 
%
4      5                Pakistan          220892340  ...       23         35 %       2.83 
%
..   ...                     ...                ...  ...      ...          ...          ...
230  231              Montserrat               4992  ...     N.A.         10 %       0.00 
%
231  232        Falkland Islands               3480  ...     N.A.         66 %       0.00 
%
232  233                    Niue               1626  ...     N.A.         46 %       0.00 
%
233  234                 Tokelau               1357  ...     N.A.          0 %       0.00 
%
234  235                Holy See                801  ...     N.A.         N.A.       0.00 
%

[235 rows x 12 columns]
  • Related