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.

Posted August 27, 2018 959 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(’’)
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, country, president and prime minister in the order given on site. Please give correct code to end up with the desired output.

0 answers
Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!