Reputation: 591
I built a web scraper that pulls back articles and puts them in .csv using pandas. I created a new column called "datetime" and I want the time within the column to increase in 15 minute increments. Currently, the time is all the same in all the rows e.g.
11/9/2018 20:49
11/9/2018 20:49
11/9/2018 20:49
11/9/2018 20:49
No matter when I run this script, I want to the current date and current time to populate the column in 15min increments. How do I increase the time in 15 minute increments?
date1 = datetime.datetime.now() + datetime.timedelta(minutes=15)
date2 = date1.strftime('%m/%d/%Y %H:%M:%S')
keywords = ["bitcoin", "ethereum"]
date = pd.Timestamp("today").strftime("%Y-%m-%d")
url = ('https://newsapi.org/v2/everything?q=' + ' OR '.join(keywords)) + '&language=en' + '&from=' + date +
response = requests.get(url)
json = response.json()
filename = "Nov9.csv"
df = pd.DataFrame(json['articles'])
df['datetime'] = date2
df = df[['datetime','title', 'url', 'author', 'content', 'description', 'publishedAt', 'source', 'urlToImage']
df.to_csv((filename))
Upvotes: 1
Views: 1075
Reputation: 3926
Here is how you can obtain a range of datetimes with 15 mins increment starting with current time:
import pandas as pd
pd.date_range(pd.to_datetime('today'), periods=10, freq='15min')
You can play with the rounding/formatting etc. and add it to the column.
Upvotes: 1