Apr-26-2020, 08:32 AM
Hi, guys, I need help with this code please help. Right now my data is missing and This is what I am getting right now.
--------------------------------------
,Total,Change in last 24 hours
Number of recovered cases,1142
Number of recovered cases,1142,24
------------------------------------------
But want my output like this
--------------------------------------------
,Total,Change in last 24 hours
Number of confirmed cases in New Zealand,1121,4
Number of probable cases,349,5
Number of confirmed and probable cases,1470,9
Number of cases currently in hospital,7,0
Number of recovered cases,1142,24
Number of deaths,18,0
---------------------------------------------------
This is my python code
--------------------------------------
,Total,Change in last 24 hours
Number of recovered cases,1142
Number of recovered cases,1142,24
------------------------------------------
But want my output like this
--------------------------------------------
,Total,Change in last 24 hours
Number of confirmed cases in New Zealand,1121,4
Number of probable cases,349,5
Number of confirmed and probable cases,1470,9
Number of cases currently in hospital,7,0
Number of recovered cases,1142,24
Number of deaths,18,0
---------------------------------------------------
This is my python code
import requests from bs4 import BeautifulSoup import csv result = requests.get("https://www.health.govt.nz/our-work/diseases-and-conditions/covid-19-novel-coronavirus/covid-19-current-situation/covid-19-current-cases") src = result.content soup = BeautifulSoup(src,'lxml') cov19_table = soup.find("table", attrs={"class": "table-style-two"}) cov19_table_head = cov19_table.find_all("thead") cov19_table_data = cov19_table.tbody.find_all("tr") head = [] for th in cov19_table_head[0].find_all("th"): head.append(th.text.replace('\n', '').strip()) num = 0 while num <= 5: with open('nt.csv', 'w', newline="") as new_file: data = {} headings = [] csv_writer = csv.writer(new_file, delimiter=',') csv_writer.writerow(head) for th in cov19_table_data[num].find_all("th"): headings.append(th.text.replace('\n', '').strip()) t_data = [] for td in cov19_table_data[num].find_all("td"): value = td.text.strip().replace(',', '') t_data.append(int(value)) data = headings + t_data print(num) num = num+1 #csv_writer = csv.writer(new_file, delimiter=',') #csv_writer.writerow(head) csv_writer.writerow(data) print(data)