i have a HTML Table with 5 columns. In the third column are links and in the fifth column is a Date.
Now i want to write a code which check if the date is within the next 4 weeks and if yes, then click on the link
This is what i have so far:
# Set the Date
start = time.strftime('%d-%m-%Y')
now = datetime.datetime.strptime(start, '%d-%m-%Y')
date_in_four_weeks = now + datetime.timedelta(days=28)
project_time = date_in_four_weeks - now
# Check the date and click on link
for i in range(project_time.days + 1):
print(now + timedelta(days=i))
time = driver.find_element_by_css_selector('css_selector')
if time <= project_time:
linkList = driver.find_elements_by_css_selector("css_selector")
for i in range(0,len(linkList)):
links = driver.find_elements_by_partial_link_text('SOLI')
links[i].click()
driver.get_screenshot_as_file("test.png")
else:
print "No Project found"
If i run the code i get the error:
TypeError: can't compare datetime.timedelta to WebElement
Now i want to ask if there is any way how i can fix my problem?
Thanks :)
There's a few issues you have to address.
Firstly, you're comparing to a WebElement object, as the error helpfully points out. This includes the tags and such of the HTML element you're referencing. You first want to extract the text.
Then, you need to parse this text to convert it into a Python datetime or date object. A time object won't do because it only stores time, and not date. Since I don't know what format your HTML date is in, I'll just point you to the docs so you can see how the types work and have some idea of how to parse your data.
Finally, you'd still get an error because of trying to compare a timedelta object to a date or datetime object. A timedelta is a period of time, it doesn't relate to a specific date.
You could fix this by replacing
if time <= project_time:
With the same as from your print function:
if time <= now + timedelta(days=i)
Related
I have a list in python which has below data . each data represent a table name
[tablename_20211011, tablename_20201010, tablename_20211009,
tablename_20211009, tablename_20211008]
20211011 -- this is the date when table got created
how i can fetch the table names which are created in last 1 year python.
if crteria is 1 yr then result should be tablename_20211011,tablename_20211009, tablename_20211008,tablename_20211009
!!!works!!! here you dont need to mention last year date manually it does the job automatically
from datetime import date
(datetime.datetime.now() - datetime.timedelta(days=365)).strftime("%Y%m%d")
d1 = today.strftime("%Y%m%d")
#this gives you date of last year
[x for x in a if x[:-8]>=d1]
this returns the items after the given date
Assuming that it is always the last 8 characters of your filename that are the date (ie. YYYYMMDD format), you could just use:
files = ['tablename_20211011', 'tablename_20201010', 'tablename_20211009', 'tablename_20211009', 'tablename_20211008']
print ([x for x in files if x[-8:] >= '20210101'])
Simply set the date-string to the right of the >= symbol as needed.
If the date is not always the last 8 characters of the string, then you may need to use a regular expression (regex) approach to extract it.
I don't want to change the code each quarter/year. How can I transform my function so it simply selects the latest file and retrieves a specific value - so in the example below, it would be the ['2020-12-31'] that is altered. Thank you
def freeCashFlow():
for r in range(len(cleanstockdata2)):
try:
if (cleanstockdata2[r]['Financials']['Cash_Flow']['yearly']['2020-09-30']["freeCashFlow"]) in ['0','',None]:
print (0)
else:
print (cleanstockdata2[r]['Financials']['Cash_Flow']['yearly']['2020-09-30']["freeCashFlow"])
except KeyError:
print (0)
You can convert the values in the json to datetime objects and just take the max of them, for instance:
from datetime import datetime as dt
date_list = [dt.strptime(key, '%Y-%m-%d') for key in cleanstockdata2[r]['Financials']['Cash_Flow']['yearly'].keys()]
max_date = dt.strftime(max(date_list), '%Y-%m-%d')
desired_value = cleanstockdata2[r]['Financials'][max_date]["freeCashFlow"]
I am new to functions and I am trying to write a function that returns the number of days between two dates:
My attempt:
import datetime
from dateutil.parser import parse
def get_x_days_ago (date_from, current_date = None):
td = current_date - parse(date_from)
if current_date is None:
current_date = datetime.datetime.today()
else:
current_date = datetime.datetime.strptime(date_from, "%Y-%m-%d")
return td.days
print(get_x_days_ago(date_from="2021-04-10", current_date="2021-04-11"))
Expected outcome in days:
1
So there seem to be multiple issues, and as I said in the comments, a good idea would be to separate the parsing and the logic.
def get_x_days_ago(date_from, current_date = None):
if current_date is None:
current_date = datetime.datetime.today()
return (current_date - date_from).days
# Some other code, depending on where you are getting the dates from.
# Using the correct data types as the input to the get_x_days_ago (datetime.date in this case) will avoid
# polluting the actual logic with the parsing/formatting.
# If it's a web framework, convert to dates in the View, if it's CLI, convert in the CLI handling code
date_from = parse('April 11th 2020')
date_to = None # or parse('April 10th 2020')
days = get_x_days_ago(date_from, date_to)
print(days)
The error you get is from this line (as you should see in the traceback)
td = current_date - parse(date_from)
Since current_date="2021-04-11" (string), but date_from is parsed parse(date_from), you are trying to subtract date from the str.
P.S. If you have neither web nor cli, you can put this parsing code into def main, or any other point in code where you first get the initial strings representing the dates.
It looks like you're already aware that you can subtract a datetime from a datetime. I think, perhaps, you're really looking for this:
https://stackoverflow.com/a/23581184/2649560
I want to extract the sunrise hour and if I do the following
sun = ephem.Sun()
r1 = home.next_rising(sun)
print ("Visual sunrise %s" % r1)
risehr = r1[10:12]
print ("Rise Hour = %s" % risehr)
I got the error
>>'ephem.Date' object has no attribute '__getitem__'
I can print the string r1 but not extract from it (?)
I tried solutions from similar problem posts on extraction but couldn't make any progress, apologies if this appears to be a double post.
As I understand your question you want to print only hour of sunrise. r1 is object of ephem.Date type. You could make it with brute force
...
risehr = str(r1)[10:12]
...
or you could convert r1 object to datetime, and datetime to str representation
...
risehr = r1.datetime().strftime('%H')
...
or convert it to tuple first
...
risehr = r1.tuple()[3]
...
All available options you could read at this page in section Conversions.
#kvorobiev answered the question of how to extract from a string representation of your data. But the other half of your question was the error:
'ephem.Date' object has no attribute '__getitem__'
According to the PyEphem documentation for the next_rising() function,
If the search is successful, returns a Date value.
Furthermore, Date objects have an important property:
Dates are stored and returned as floats. Only when printed, passed to str(), or formatted with '%s' does a date express itself as a string giving the calendar day and time.
When you gave the command risehr = r1[10:12], the Python interpreter attempted to get call Date.getattr() to get the fields from a Date object corresponding to the slice 10:12. Without that method, slicing has no meaning to a Date object.
But all is not lost! You can still get the Date object's time information:
Call .tuple() to split a date into its year, month, day, hour, minute, and second.
You can then slice this tuple as needed to get the hour:
hour = r1.tuple()[3]
Or exhaustively:
year, month, day, hour, minute, second = r1.tuple()
I've been trying to write code to scrape the number of hits within a certain date range on google. I've done this by inserting the date into the google search query. When I copy and paste the link it produces, it gives me the correct query, but when the code runs it, I keep getting the number of hits for the search without the date range. I'm not sure what I'm doing wrong here.
from bs4 import BeautifulSoup
import requests
import re
from datetime import date, timedelta
day = date.today()
friday = day - timedelta(days=day.weekday() + 3) + timedelta(days=7)
word = "debt"
for n in range(0,32,7):
date_end = friday - timedelta(days=n)
date_beg = date_end - timedelta(days=4)
link_beg = "https://www.google.com/search?q=%s&source=lnt&tbs=cdr%%3A1%%2Ccd_min%%3A" % (word)
link_date = "%s%%2F%s%%2F%s%%2Ccd_max%%3A%s%%2F%s%%2F%s&tbm=&gws_rd=ssl" % (str(date_beg.month),str(date_beg.day),str(date_beg.year),str(date_end.month),str(date_end.day),str(date_end.year))
url = link_beg + link_date
print url,
print "\t",
r = requests.get(url)
soup = BeautifulSoup(r.content)
products = soup.findAll("div", id = "resultStats")
result = str(products[0])
results = re.findall(r'\d+', result)
number = ''.join([str(i) for i in results])
print number
For example, one of the links that is produced is this:
Google Search for "debt" in date range "3/9/2015 to 3/13/2015"
The hits produced should be: 39,700,000
But instead, it spits out: 293,000,000 (which is what just a generic search produces)
Google's date range limited search relies on Julian dates-- i.e. the range must be specified in Julian nomenclature. Perhaps you realized this already.
cute kitties daterange:[some Julian date]-[another Julian date] (without brackets).
There are web pages to convert to Julian, or use the jDate Python script or jday shell script.