【问题标题】:How to get the curent date with python如何使用python获取当前日期
【发布时间】:2020-05-14 07:00:50
【问题描述】:

我用 python 3 和 bs4 做了一个网络爬虫。我想要当前日期,以便我可以将其用作抓取网站的文件名。

这是我的代码:

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()

经过几个小时的谷歌搜索,我想出了这个:

>>> import datetime
>>> print (datetime.datetime.today()) 
2020-05-14 11:49:55.695210
>>>

但是, 我想要这样的东西:2020 年 5 月 14 日 如果可以的话可以吗,请帮帮我

我只想知道当前日期为字符串

【问题讨论】:

    标签: python-3.x date web-scraping beautifulsoup python-requests


    【解决方案1】:

    使用时间模块中的strftime 函数:

    import time
    time.strftime("%d-%B-%Y", time.localtime())
    
    '14-May-2020'
    

    【讨论】:

      猜你喜欢
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 2023-02-08
      • 2020-09-19
      • 1970-01-01
      相关资源
      最近更新 更多