Im new to python and im trying to collect data from a website. My issue now is im stuck in the last flow where I want to take the data and iterate it to Pandas dataframe before saving it in a database/csv file.
I tried to append the data using loop but it seems that my loop is not working. if you can see if i view "cols" I managed to clean the data up but it not come into the table.
import requests, pandas, numpy, matplotlib.pyplot
from bs4 import BeautifulSoup
#### page info ###
page = requests.get("https://postcode.my/search/?keyword=&state=Kedah")
#### check page status (will come 200 if the page is ok)
page.status_code
### call Library
soup = BeautifulSoup(page.content, 'html.parser')
### Find rows
rows = soup.find_all(class_="col-lg-12 col-md-12 col-sm-12 col-xs-12")
## define column
LOCATION = []
AREA = []
STATE = []
POSTCODE = []
TABLE = []
counter= 0
for row in rows:
cols = row.find_all("td")
cols = [x.text.strip() for x in cols]
if cols!='':
TABLE.append(cols)
counter=counter+1
if counter == 4:
LOCATION.append(TABLES[0])
AREA.append(TABLE[1])
STATE.append(TABLE[2])
POSTCODE.append(TABLE[3])
counter = (0)
TABLE = []
PDTABLE = pandas.DataFrame({
"LOCATION" : LOCATION,
"AREA" : AREA,
"STATE" : STATE,
"POSTCODE" : POSTCODE
})
PDTABLE
Thank You Best Regards Railey Shahril