Reputation: 4008
I've found this other question to fetch a date when scraping a website.
However, the solution provided gives me a integer representation, when the desire ouput is a date.
Prefered format: 09 de julio del 2019
from bs4 import BeautifulSoup
ec_editorial = requests.get("https://elcomercio.pe/opinion/editorial")
ec_editorial_scr = ec_editorial.content
data = """your html goes here"""
soup = BeautifulSoup(ec_editorial_scr)
for i in soup.findAll('time'):
if i.has_attr('datetime'):
print(i['datetime'])
prints
1560076500
1559990100
1559990100
Upvotes: 0
Views: 231
Reputation: 2416
1560076500, 1559990100 are epoch time, i.e. the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).
Easiest way to convert those into string format are using Python time
library.
localtime
.>>> date = 1560076500
>>> import time
>>> date = time.localtime(date)
>>> date
time.struct_time(tm_year=2019, tm_mon=6, tm_mday=9, tm_hour=16, tm_min=5, tm_sec=0, tm_wday=6, tm_yday=160, tm_isdst=0)
strftime()
to format into string.>>> time.strftime('%d %b %Y', date)
'09 Jun 2019'
Upvotes: 1
Reputation: 23815
Here (You can 'play' with the format string in order to get the exact output)
import time
import requests
from bs4 import BeautifulSoup
ec_editorial = requests.get("https://elcomercio.pe/opinion/editorial")
soup = BeautifulSoup(ec_editorial.content, 'html.parser')
for i in soup.findAll('time'):
if i.has_attr('datetime'):
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(i['datetime']))))
output
2019-06-09 13:35:00
2019-06-08 13:35:00
2019-06-08 13:35:00
2019-06-07 13:35:00
2019-06-06 13:35:00
....
Upvotes: 1
Reputation: 17368
Assuming 1560076500 are unix timestamps,
import datetime
time_stamp = 1559990100
converted_date = datetime.datetime.fromtimestamp(time_stamp / 1e3)
print(converted_date)
print(str(converted_date)
Output:
datetime.datetime(1970, 1, 19, 6, 49, 50, 100000)
'1970-01-19 06:49:50.100000'
Upvotes: 1