trubex
trubex

Reputation: 139

Scrape Wikipedia table to CSV in Python

I'm scraping a table from Wikipedia using python. I'm done with the code, but I'm having some issues extracting specific columns to CSV, and adding enclosing double apostrophes.

I want to extract for only the following column names:

Kode BPS, Nama, Ibu Kota, Populasi, Luas, Pulau.

Here's the result of the table:

enter image description here

And here's my code:

import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = 'https://id.wikipedia.org/wiki/Demografi_Indonesia'
response = requests.get(URL)
soup = BeautifulSoup(response.text,'html.parser')

table = soup.find('table',{'class':'wikitable sortable'}).tbody
rows = table.find_all('tr')
columns = [v.text.replace('\n','') for v in rows[0].find_all('th')]

df = pd.DataFrame(columns=columns)

for i in range(1,len(rows)):
    tds = rows[i].find_all('td')

    if len(tds)==4:
        values = [tds[0].text, tds[1].text, tds[2].text, tds[3].text.replace('\n',''.replace('\xa0',''))]
    else:
        values = [td.text.replace('\n',''.replace('\xa0','')) for td in tds]

    df = df.append(pd.Series(values, index=columns), ignore_index=True)
    #print(df)

    df.to_csv(r'C:\Users\Desktop\'+'\\report.csv',index=False)

Upvotes: 5

Views: 4283

Answers (3)

Jonathan Rolfsen
Jonathan Rolfsen

Reputation: 53

To convert Wikipedia page's tables to CSV (pd.read_html()) (df.empty) (df.to_csv()):

import pandas as pd

def wiki_to_csv(wikiurl = str):
    tname  = link.split("/")[-1]
    tables = pd.read_html(link, header=0)

    for i in range(len(tables)):
        if not tables[i].empty:
            fname = tname + " table " + str(i)
            tables[i].to_csv(fname, sep=',')

To scrape that exact table and select specific columns (df.rename()) (Select Columns):

import pandas as pd

link = "https://id.wikipedia.org/wiki/Demografi_Indonesia"
df = pd.read_html(link, header=0)[2]

df = df.rename(columns={'Populasi[4]':'Populasi', 'Luas (km²)[5]':'Luas'})
df = df[['Kode BPS', 'Nama', 'Ibu kota', 'Populasi', 'Luas', 'Pulau']]

df.to_csv("Indonesia.csv", sep=',')

I'm not sure what issue you are having with double quotes.

Upvotes: 1

ASH
ASH

Reputation: 20322

How about this?

import pandas as pd
link = "https://id.wikipedia.org/wiki/Demografi_Indonesia"
tables = pd.read_html(link,header=0)[2]
df.to_csv(tables, sep='\t')

Keep it simple.

Upvotes: 4

Sanip
Sanip

Reputation: 1810

You can specify the columns in the dataframe as:

columns = ['Kode BPS', 'Nama', 'Ibu Kota', 'Populasi', 'Luas', 'Pulau']
df = pd.DataFrame(columns=columns)

Then just insert the values required.

Upvotes: 1

Related Questions