Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup as soup
  3. import csv
  4. import datetime
  5.  
  6. date = datetime.datetime.today() #datetime object for formating name of the output file
  7. output_f = 'covid19singapore_{year}-{month}-{day}({hour}-{minute}).csv'.format(
  8.                                                                               year= date.year,
  9.                                                                               month=,date.month,
  10.                                                                               day = date.day,
  11.                                                                               hour = date.hour,
  12.                                                                               minute = date.minute,
  13.                                                                              
  14.  
  15. data_dict = {
  16.       'Case': '',
  17.       'Patient': '',
  18.       'Age': '',
  19.       'Gender': '',
  20.       'Nationality': '',
  21.       'Status': '',
  22.       'Infection_Source': '',
  23.       'Country_of_Origin': '',
  24.       'Symptomatic_to_Confirmation': '',
  25.       'Days_to_Recover': '',
  26.       'Symptomatic_At': '',
  27.       'Confirmed_At': '',
  28.       'Recovered_At': '',
  29.       'Displayed_Symptoms': '',
  30.       'Details': '',
  31.       'Sources': '',
  32.       'URL': '',
  33. }
  34.  
  35. def get_tree(url):
  36.    url = url
  37.    r = requests.get(url)
  38.    return soup(r.text, 'html.parser')
  39.  
  40. def details_sources(url):
  41.    supa = get_tree(url)
  42.    Details = supa.find(class_='col-lg-8 col-xl-9 mg-t-10')
  43.    Sources = supa.find(class_='card mg-t-10')
  44.    return [Sources.text.replace('Sources','').strip(), Details.text.replace('Details','').strip()]
  45.  
  46. supa = get_tree('https://www.againstcovid19.com/singapore/cases/search')
  47. tabular = supa.find_all('tr')
  48. fieldnames = data_dict.keys()
  49.  
  50. with open(output_f,'w', newline='') as f:
  51.    csv_writer = csv.DictWriter(f)
  52.    for i,row in tabular:
  53.       tabs = row.find_all('td')
  54.       for it,tab in enumerate(tabs):
  55.          if it == 0:
  56.             a = tab.find('a')
  57.             print(a['href'])
  58.             print(tab.text.strip())
  59.          else:
  60.             print(tab.text.strip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement