By Joash Ouso
import requests from bs4 import BeautifulSoup import csv
f = csv.writer(open(‘world-presidents.csv’, ‘w’)) f.writerow([‘S.No’, ‘Country’,‘President’,‘Prime-Minister’])
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’)
president_name_list = soup.find(class_=‘content’)
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’)
for president_name in president_name_list_items: names = president_name.contents[0]
f.writerow([names])
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!
Hi there,
It seems like you want to write the scraped data into a CSV file with columns: S.No, Country, President, and Prime-Minister. Here’s a modified version of your code that should work:
import requests
from bs4 import BeautifulSoup
import csv
# Create a CSV file and write the header
with open('world-presidents.csv', 'w', newline='') as csvfile:
f = csv.writer(csvfile)
f.writerow(['S.No', 'Country', 'President', 'Prime-Minister'])
# Collect and parse the 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
content = soup.find(class_='content')
# Find the table and extract rows
table = content.find('table')
rows = table.find_all('tr')
# Iterate through rows, skipping the header
for row in rows[1:]:
columns = row.find_all('td')
# Extract data from columns
s_no = columns[0].get_text().strip()
country = columns[1].get_text().strip()
president = columns[2].get_text().strip()
prime_minister = columns[3].get_text().strip()
# Write data to the CSV file
f.writerow([s_no, country, president, prime_minister])
This code will write the data to the CSV file with the specified columns. It first finds the table within the ‘content’ div, then iterates through each row, extracting the data from each column, and writes it to the CSV file.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.