R
R
r4khic2019-08-22 09:19:31
Python
r4khic, 2019-08-22 09:19:31

How can I convert the date to the format I need?

Hello ! I am parsing the date of one page. And here is the result of parsing

2019-08-22T00:01:00Z

The code:
def get_data(html):
    soup = BeautifulSoup(html, 'lxml')
    item_datetime=soup.find('meta',{'itemprop':'dateCreated'})
    item_datetime=dateparser.parse(item_datetime,date_formats=['%d %B %Y %H'])
    print(item_datetime)

This format is not suitable for my database. I decided to use the dateparser library. But the essence of the problem is that Dateparser does not understand this date 2019-08-22T00:01:00Z I understood this judging by the text of the error:
File "C:/Users/Administrator/PycharmProjects/Task/parsers/bnewskz/bnewskz_parser.py", line 13, in get_data item_datetime=dateparser.parse(item_datetime,date_formats=['%d %B %Y %H'])
raise TypeError('Input type must be str or unicode') TypeError: Input type must be str or unicode
Process finished with exit code 1

How to convert date?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alternativshik, 2019-08-22
@alternativshik

from dateutil import parser
item_datetime = parser.parse('2019-08-22T00:01:00Z')

well, or native strptime()

A
Alexey Guest007, 2019-08-22
@Guest007

soup.find returned the date already, most likely in date format. Just take it out the way you want it. And you're trying to parse it as a string. It is about this (that item_datetime is not a string) that is written to you in the error.
easy to check - insert print(type(item_datetime)) after soup.find

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question