-2

I have seen that recently table for List of postal codes of Canada on Wikipedia has changed from normal table to common grid style List of postal codes of Canada: M.

I tried in different way but its not easy to convert it into df.

Problem with this data is it is values are not separated by tr tag.I want a df in the old format of data like Old format

My question is - can i do this with bs4 or any python code directly or I have to import table as it is and then modify df. If there is easy way please help me.

My code gives result like this :

import requests
from bs4 import BeautifulSoup
req = requests.get("https://en.wikipedia.org /wiki/List_of_postal_codes_of_Canada:_M")

soup = BeautifulSoup(req.content,'lxml')

table = soup.find_all('table')[0]

print(table.tr.text)
df = pd.read_html(str(table))
df=pd.DataFrame(df[0])

result

Apurv
  • 13
  • 3

1 Answers1

0
import pandas as pd

df = pd.read_html(
    "https://en.wikipedia.org/w/index.php?title=List_of_postal_codes_of_Canada:_M")[0]


df.to_csv("data.csv", index=False)

Output: view-online

enter image description here

  • Right I am getting the same output but i want output in the old format like this : [link](https://en.wikipedia.org/w/index.php?title=List_of_postal_codes_of_Canada:_M&oldid=945633050) – Apurv Mar 28 '20 at 09:51