-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDate_time_scraper.py
59 lines (52 loc) · 2.41 KB
/
Date_time_scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# extracting time and date from the below website
# https://www.timeanddate.com/
"""Python Program to Quickly fetch date and time from https://www.timeanddate.com/ and copy it to clipboard"""
from bs4 import BeautifulSoup
import requests
import re
import time
from _datetime import datetime
import pyperclip as pc
def filter_date(date_string):
Match_string = re.match(f"^(\S*), (\d*) (\D*) (\d*)", date_string)
week_day = Match_string.group(1)
date2 = Match_string.group(2)
month = Match_string.group(3)
year = Match_string.group(4)
return week_day, date2, month, year
def filter_time(time_string):
y_time = re.match('(\d*):(\d*):(\d*)', time_string)
hr = y_time.group(1)
minute = y_time.group(2)
sec = y_time.group(3)
return hr, minute, sec
if __name__ == '__main__':
country = 'india' # input('Enter your country:')
city = 'kolkata' # input('Enter your city:')
# url = 'https://www.timeanddate.com/worldclock/india/kolkata'
url = f'https://www.timeanddate.com/worldclock/{country}/{city}'
headers = {"Accept-Language": "en-US,en;q=0.5"}
r = requests.get(url, headers=headers)
print(f'Status Code: {r.status_code}') # permission status code, 200 - allowed, other wise = not allowed
data = r.content
soup = BeautifulSoup(data, "html5lib")
# print(soup.prettify())
time = soup.find_all('span', {"class": "h1"})[0].text # gets the current local time from the given website
print(f'Current local time: {time}')
date = soup.find_all("span", {"id": "ctdat"})[0].text # gets the current local date from the given website
print(f'Current local date: {date}')
week_day, day, mnth, year = filter_date(date)
hr, minute, sec = filter_time(time)
# print(f'{day},{mnth},{year},{hr},{minute},{sec}')
week_days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sept', 'oct', 'nov', 'dec']
for i in months:
if mnth.lower().startswith(i):
mnth = months.index(i) + 1
break
# print(f'month={mnth}')
week_day_no = week_days.index(week_day.lower()) + 1
time_tuple = (int(year), int(mnth), week_day_no, int(day), int(hr), int(minute), int(sec), 0)
# just copy the date and time to the clip board
pc.copy(time + '.0')
print("Time copied!")