Reputation: 443
I have made a web-scraper with python 3 and bs4. I want the current date so that i can use it as a file name for the scraped website.
Here is my code:
import bs4
import requests
import sys
import re
import unicodedata
import os
filename = #Current date#
filename=r"C:\Python\Scripts\Webscrapers\Output\\" +filename+ ".txt"
url = "https://www.wikipedia.org/Example_Article/"
res = requests.get(url)
soup = bs4.BeautifulSoup(res.text, "lxml")
file = open(filename , 'wb')
for i in soup.select("p"):
f=i.text
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", f)).encode('ascii', 'ignore'))
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", os.linesep)).encode('ascii', 'ignore'))
file.write(unicodedata.normalize('NFD', re.sub("[\(\[].*?[\)\]]", "", os.linesep)).encode('ascii', 'ignore'))
file.close()
After hours of googling i came up with this:
>>> import datetime
>>> print (datetime.datetime.today())
2020-05-14 11:49:55.695210
>>>
But, I want something like this: 14-May-2020 Is it possible if so, then please help me
I just want to know the current date as a string
Upvotes: 0
Views: 52
Reputation:
Use the strftime function from the time module:
import time
time.strftime("%d-%B-%Y", time.localtime())
'14-May-2020'
Upvotes: 2