我想从Google新闻中的最新文章中获取数据
我开始在Python中使用datetime包,并希望格式化今天的日期
from datetime import date
today =date.today()
print(today)
>> 2016-12-10
采用以下格式:
December 10
" 12月"是一个字符串和" 10"作为一个整数,以循环
December 10
December 9
December 8
December 7
在日期与Google结果匹配之前
import requests
link = "https://www.google.com.mx/search?biw=1535&bih=799&tbm=nws&q=%22New+Strong+Buy%22+site%3A+zacks.com&oq=%22New+Strong+Buy%22+site%3A+zacks.com&gs_l=serp.3...1632004.1638057.0.1638325.24.24.0.0.0.0.257.2605.0j15j2.17.0....0...1c.1.64.serp..8.0.0.Nl4BZQWwR3o"
fetch_data =requests.get(link)
content = str((fetch_data.content))
答案 0 :(得分:0)
最简单的方法是创建一个包含每个月字符串的列表,然后使用today.month
和today.day
来获取所需的数字。
from datetime import date
Months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
today = date.today()
print("%s %s" % (Months[today.month - 1],today.day))
注意:today.day
默认为整数,就像today.month