Environment:
distro:os X 10.12.3
python interpreter: 3.6.1
recently wrote a news aggregation app, in which the server uses Python to crawl RSS data from major news sites. One of the problems encountered is the unification of date formats:
Sina News's RSS date format is this:
<pubdate>sun, APR 2017 05:15:05 gmt</pubdate>
Baidu News of the date format of the RSS is this:
<pubdate><! [cdata[2017-04-23t05:27:20.000z]]></pubdate>
I was hoping to unify the date with Gus for 2017-04-23 11:11:11. I found this interface.
Datetime.datetime.strptime ()
The question is how to use it, see the code directly to understand:
Import datetime #python3解释器自带datetime模块
newstime= ' Sun, APR 2017 05:15:05 GMT '
gmt_format = '%a,%d%b%Y: %s GMT '
newstime=datetime.datetime.strptime (newstime,gmt_format)
print (newstime) #2017-04-23 05:15:05
We succeeded in getting the result, and we can see that the key to the problem is the second parameter of the interface: Gmt_format: As long as the Gmt_format match the original date format
, GMT represents GMT, if you want to convert to Beijing time, plus 8 hours:
Newstime=datetime.datetime.strptime (Newstime,bai_format) +date.timedelta (hours=8)
Baidu news site's RSS time format left to the reader's own conversion.
Attention:
The placeholders for apr,sept, such as month, are%b, and 03,04 these month digits are%m