The date data scraped on the WEB is a character string (str).
Convert the character string to date in order to store the acquired data in DB as date data.
Use the strptime method of the datetime module.
python
import datetime as dt
No definition
import datetime
datetime.datetime.now()
#It is necessary to describe the module name at the beginning in full
With definition
import datetime
dt.datetime.now()
#The module name at the beginning can be abbreviated
** How to use strptime ('A','B') ** -Match "A character string" and "B format".
Example 1(0 not filled)
A:「2019-1-9」
B:「%Y-%m-%d」
dt.datetime.strptime('2019-1-9','%y-%m-%d')
#output
datetime.datetime(2019, 1, 9, 0, 0)
% Y: 4 digits in the Christian era % m: Month (character string month is padded with 0 or not) % d: Day (character string month is padded with 0 or not)
Example 2(0 padded)
A:「19-05-08」
B:「%y-%m-%d」
dt.datetime.strptime('19-05-08','%y-%m-%d')
#output
datetime.datetime(2019, 5, 8, 0, 0)
% y: 2 digits (lowercase) % m,% d: 0 Supports both with and without padding
Example 3(date)
A: "May 8, 2019"
B:「%Y year%m month%d day "
dt.datetime.strptime('May 8, 2019','%Y year%m month%d day')
#output
datetime.datetime(2019, 5, 8, 0, 0)
Example 4(Correspondence between character strings and numerical values)
A: "Tomorrow's date is May 8th. The year is 2019."
B: "Tomorrow's date is%m month%d days. Year is%Y year "
dt.datetime.strptime('Tomorrow's date is May 8th. Year is 2019','Tomorrow's date%m month%d days. Year is%Y year')
#output
datetime.datetime(2019, 5, 8, 0, 0)
・ Dates are not in order -Just specify which number will be the date data
Example 5(variable)
A:「past」:'2019/3/23'
B:「%Y/%m/%d」
past= '2019/3/23'
dt.datetime.strptime(past,'%Y/%m/%d')
#output
datetime.datetime(2019, 3, 23, 0, 0)
Error when quoting variables ValueError: time data 'past' does not match format '%Y/%m/%d'
Example 6(Time,Minutes,Including seconds)
A:「future」:'March 23, 2030 5:21:42 123456 microseconds'
B:「%Y year%m month%d day%H o'clock%M minutes%S seconds%f microseconds "
past= '2019/3/23'
dt.datetime.strptime(future, '%Y year%m month%d day%H o'clock%M minutes%S seconds%f microseconds')
#output
datetime.datetime(2030, 3, 23, 5, 21, 42, 123456)
% H: Time (24-hour notation) % M: minutes % S: seconds % f: microseconds
code
dt.datetime.now()
#output
# datetime.datetime(2020, 3, 23, 0, 18, 1, 735003)
In the first place, it was a mistake to think that "datetime.datetime.now ()" was divided into three parts, "①datetime", "②.datetime", and "③now ()".
Correctly, it is divided into two parts, "(1) datetime.datetime" and "(2) now ()".
The syntax for using the method is object
. method
, so it looks like this:
Object
: ① datetime.datetime
Method
: ② now ()
Put an entity in the object.
**
→ datetime.datetime is an instance
It's hard to understand if you just look at "datetime.datetime", but there are other types.
datetime.date
: date
datetime.time
: time
datetime.timedelta
: difference
datetime.tzinfo
: Time zone information
Such
▶「datetime.datetime.now()」
datetime.datetime
is a collection of functions related to" date and time ".
The now ()
method calls the now () function defined in datetime.datetime
.
▶「datetime.date.now()」
** Error **.
datetime.date
is a collection of functions related to" time ". The function now ()
is not defined in this.
▶「datetime.time.today()」
You can use this.
A function called today ()
is defined in the class of datetime.date
.
--Multiple classes are defined in the datetime module. --Specify the class with the data you want to handle (date, time, both? Etc.) --Class (instance) specification: datetime.AAAA --The methods defined for each class are different. --Use the method that suits each class: datetime.AAAA.BBBB ()
datetime.time.today() datetime.datetime.today()
Recommended Posts