আমাদের ওয়েব পেজটি হলো
https://www.worldometers.info/population/countries-in-asia-by-population/
আমাদের ওয়েব পেজটি দেখতে এমন

প্রথমে রিকোয়েস্ট পাঠাই
import requests
import pandas as pd
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/population/countries-in-asia-by-population/'
response = requests.get(url)
soup = BeautifulSoup(response.content,'html.parser')
এবার আমরা ডেটা গুলোকে সিলেক্ট করে ধরবো
H1 সিলেক্ট করি
h1 = soup.find('h1').text
Output
Asian Countries by population (2023)
div এর মধ্যে থেকে টেবিল সিলেক্ট করি
tablediv = soup.find('div',{'class':'table-responsive'})
টেবিল সিলেক্ট করি
countrytable = tablediv.find('table')
টেবিল tbody সিলেক্ট করি
tablebody = tablediv.find('tbody')
টেবিল tbody row সিলেক্ট করি
rows = tablebody.find_all('tr')
country = []
population = []
Loop the table
country = []
population = []
for row in rows:
td1 = row.find_all("td")[1].text.strip()
td2 = row.find_all("td")[2].text.strip()
country.append(td1)
population.append(td2)
print(country)
print(population)
Output
['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burundi', "Côte d'Ivoire", 'Cabo Verde', 'Cambodia', 'Cameroon', 'Canada', 'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Congo (Congo-Brazzaville)', 'Costa Rica', 'Croatia', 'Cuba', 'Cyprus', 'Czechia (Czech Republic)', 'Democratic Republic of the Congo', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Eswatini (fmr. "Swaziland")', 'Ethiopia', 'Fiji', 'Finland', 'France', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Holy See', 'Honduras', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Myanmar (formerly Burma)', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'North Korea', 'North Macedonia', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestine State', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Korea', 'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Sweden', 'Switzerland', 'Syria', 'Tajikistan', 'Tanzania', 'Thailand', 'Timor-Leste', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States of America', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe']
['38,928,346', '2,877,797', '43,851,044', '77,265', '32,866,272', '97,929', '45,195,774', '2,963,243', '25,499,884', '9,006,398', '10,139,177', '393,244', '1,701,575', '164,689,383', '287,375', '9,449,323', '11,589,623', '397,628', '12,123,200', '771,608', '11,673,021', '3,280,819', '2,351,627', '212,559,417', '437,479', '6,948,445', '20,903,273', '11,890,784', '26,378,274', '555,987', '16,718,965', '26,545,863', '37,742,154', '4,829,767', '16,425,864', '19,116,201', '1,439,323,776', '50,882,891', '869,601', '5,518,087', '5,094,118', '4,105,267', '11,326,616', '1,207,359', '10,708,981', '89,561,403', '5,792,202', '988,000', '71,986', '10,847,910', '17,643,054', '102,334,404', '6,486,205', '1,402,985', '3,546,421', '1,326,535', '1,160,164', '114,963,588', '896,445', '5,540,720', '65,273,511', '2,225,734', '2,416,668', '3,989,167', '83,783,942', '31,072,940', '10,423,054', '112,523', '17,915,568', '13,132,795', '1,968,001', '786,552', '11,402,528', '801', '9,904,607', '9,660,351', '341,243', '1,380,004,385', '273,523,615', '83,992,949', '40,222,493', '4,937,786', '8,655,535', '60,461,826', '2,961,167', '126,476,461', '10,203,134', '18,776,707', '53,771,296', '119,449', '4,270,571', '6,524,195', '7,275,560', '1,886,198', '6,825,445', '2,142,249', '5,057,681', '6,871,292', '38,128', '2,722,289', '625,978', '27,691,018', '19,129,952', '32,365,999', '540,544', '20,250,833', '441,543', '59,190', '4,649,658', '1,271,768', '128,932,753', '548,914', '4,033,963', '39,242', '3,278,290', '628,066', '36,910,560', '31,255,435', '54,409,800', '2,540,905', '10,824', '29,136,808', '17,134,872', '4,822,233', '6,624,554', '24,206,644', '206,139,589', '25,778,816', '2,083,374', '5,421,241', '5,106,626', '220,892,340', '18,094', '5,101,414', '4,314,767', '8,947,024', '7,132,538', '32,971,854', '109,581,078', '37,846,611', '10,196,709', '2,881,053', '19,237,691', '145,934,462', '12,952,218', '53,199', '183,627', '110,940', '198,414', '33,931', '219,159', '34,813,871', '16,743,927', '8,737,371', '98,347', '7,976,983', '5,850,342', '5,459,642', '2,078,938', '686,884', '15,893,222', '59,308,690', '51,269,185', '11,193,725', '46,754,778', '21,413,249', '43,849,260', '586,632', '10,099,265', '8,654,622', '17,500,658', '9,537,645', '59,734,218', '69,799,978', '1,318,445', '8,278,724', '105,695', '1,399,488', '11,818,619', '84,339,067', '6,031,200', '11,792', '45,741,007', '43,733,762', '9,890,402', '67,886,011', '331,002,651', '3,473,730', '33,469,203', '307,145', '28,435,940', '97,338,579', '29,825,964', '18,383,955', '14,862,924']
সম্পূর্ণ কোড :
import requests
import pandas as pd
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/population/countries-in-asia-by-population/'
response = requests.get(url)
soup = BeautifulSoup(response.content,'html.parser')
h1 = soup.find('h1').text
tablediv = soup.find('div',{'class':'table-responsive'}).find('table')
tablebody = tablediv.find('tbody')
rows = tablebody.find_all('tr')
country = []
population = []
for row in rows:
td1 = row.find_all("td")[1].text.strip()
td2 = row.find_all("td")[2].text.strip()
country.append(td1)
population.append(td2)
# Convert the population data into numeric format
population_numeric = [float(pop.replace(',', '')) for pop in population]
data = {'Country': country,'Population':population_numeric}
df = pd.DataFrame(data)
df.plot(kind='bar', x='Country', y='Population', legend=None)
plt.show()