Question

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.

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

president_name_list = soup.find(class_=‘content’)

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

president_name_list_items = president_name_list.find_all(‘td’) president_name_list = soup.find(class_=‘content’) president_name_list_items = president_name_list.find_all(‘td’)

Create for loop to print out all artists’ names

for president_name in president_name_list_items: names = president_name.contents[0]

f.writerow([names])
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer