I am scraping a site for world presidents' list. I have done the following code which gets the names but on the csv file I need help.

August 27, 2018 757 views
Data Analysis

import requests
from bs4 import BeautifulSoup
import csv

f = csv.writer(open('world-presidents.csv', 'w'))
f.writerow(['S.No', 'Country','President','Prime-Minister'])

Collect and parse first page

page = requests.get('http://bankersdaily.in/list-of-current-prime-ministers-president-in-world-updated-till-may-2018-static-gk')
soup = BeautifulSoup(page.text, 'html.parser')

Pull all text from the BodyText div

presidentnamelist = soup.find(class_='content')

Pull text from all instances of <a> tag within BodyText div

presidentnamelistitems = presidentnamelist.findall('td')
presidentnamelist = soup.find(class='content')
namelistitems = presidentnamelist.find_all('td')

Create for loop to print out all artists' names

for presidentname in presidentnamelistitems:
names = president_name.contents[0]

1 comment
  • I need to have a csv file that prints in excel the s.no, country, president and prime minister in the order given on site. Please give correct code to end up with the desired output.

Be the first one to answer this question.