Python Forum
Date Variables and URL Parameters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date Variables and URL Parameters
#1
Is it SIMPLY IMPOSSIBLE to convert a DATE in python into a STRING suitable for a query parameter embedded in a URL? FOR ME IT IS. I've tried adding the date to lists, to dictionaries, writing it to CSV files(because it seems you can write a date STRING to a CSV file) then reading that CSV file back in. QUESTION IS:HOW do I accept a simple date into a program, then turn around and feed that date into a long URL string with ONE parameter being that date? Also, is there some secret to all of the gobledegook special characters framing list or or dictionary entries when I simply want the kernel of DATA ONLY that is inside of those frames(I understand that they ARE necessary within list/dictionary constructs but why does a PRINT statement have to include them?) Thank you in advance for any help you can provide.
Reply
#2
You most explain better and show some data.
Is DATE that you mention a Python datetime object?
It's not hard to convert a date back and forth.
>>> from datetime import datetime
>>> 
>>> dt = datetime.now()
>>> # It's now a datetime object
>>> dt
datetime.datetime(2018, 11, 6, 19, 27, 40, 618447)
>>> type(dt)
<class 'datetime.datetime'>
>>> 
>>> # To string
>>> date_string = dt.strftime('%B %d, %Y')
>>> date_string
'November 06, 2018'
>>> 
>>> # f-string 3.6 -->
>>> date_string = f'{dt:%B %d, %Y}'
>>> date_string
'November 06, 2018' 
Quote:QUESTION IS:HOW do I accept a simple date into a program, then turn around and feed that date into a long URL string with ONE parameter being that date?
It dependents on what server will take in as data,can construct a url with a date parameter.
Often can use Requests with POST parameter to server(has often a API).
Eg:
from datetime import datetime
import requests

dt = datetime.now()
date_string = f'{dt:%B %d, %Y}'
payload = {'user_name': 'admin', 'date': date_string}
r = requests.post("http://httpbin.org/post", json=payload)
print(r.text)
json_data = r.json()
print(json_data['json']['date'])
Output:
{ "args": {}, "data": "{\"user_name\": \"admin\", \"date\": \"November 06, 2018\"}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "51", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "python-requests/2.19.1" }, "json": { "date": "November 06, 2018", "user_name": "admin" }, "origin": "46.246.118.243", "url": "http://httpbin.org/post" } November 06, 2018
Reply
#3
Thank you for the help, I will study this. I was able to work out my date problem satisfactorily. I do NOT use requests but rather urlopen to initiate conversation with my target API as follows:
myurl = urllib.request.urlopen(urlstringbuild)

urlstringbuild is a concatenated string of:

part1 = domain info and user/pass etc.
part2 = MY DATE VARIABLE WHICH NOW WORKS!
part3 = string of additional parameters

# so URLSTRINGBUILD=part1+part2+part3

# I then read the response data:
resp_data=myurl.read()

# Create a JSON dictionary from the results

json_resp=json.loads(resp_data) # API response from URL is JSON format
ALL OF THIS IS GOOD, BUT ----- when I try to ITERATE THROUGH MULTIPLE DATES, I keep getting the SAME data
back. Only the first date's data gets repeated even though I get a valid 200 response code each time I execute the
URLOPEN request. My iteration is straight forward...I ask user for a date range, populate a list containing all dates
in that range and then update my urldate parameter by looping through that list.

All seems fine, except I am not really getting the data for a different date. I loop through the correct amount of times, but no data refresh occurs.

Is there a proper way to iterate through sending repeated URL requests to a server? Do I need to clear cache somehow?

Any suggestions appreciated.

Thank you.
Reply
#4
Use code tag,i have added tags now.

Quote:populate a list containing all dates
in that range and then update my urldate parameter by looping through that list.
Looks like you would need a loop,you don't show example of how data looks so hard to say.
Eg:
lst = ['21:23:10', '21:23:20', '21:23:30']
url = 'http://something.io/{}'
for d in lst:
    # Need to do server call inside loop
    # Can also set time delay between calls
    print(url.format(d))
Output:
http://something.io/21:23:10 http://something.io/21:23:20 http://something.io/21:23:30
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 123 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format and past date check function Turtle 5 4,069 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,190 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,803 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Unable to pass date timestamp as Parameters : nagu4651 1 1,785 Jun-22-2020, 06:07 PM
Last Post: Larz60+
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,101 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  How to change existing date to current date in a filename? shankar455 1 2,273 Apr-17-2019, 01:53 PM
Last Post: snippsat
  Date format conversion "/Date(158889600000)/" lbitten 2 2,790 Nov-29-2018, 02:14 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020