Reputation: 11
I have this script and I want to replace the date 2015-05-01
(from and to) with the current date. I am thinking of using import datetime and something like this (I am new to python):
from StringIO import StringIO
import urllib
import urllib2
url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
data = "POSTDATA=FromDate=2015-05-01&ToDate=2015-06-01&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken= "
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
New script
from StringIO import StringIO
import urllib
import urllib2
import datetime
i = datetime.datetime.now() // gets the date
url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken=" //i replaced the fixed date with the variable i
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
Upvotes: 0
Views: 1068
Reputation: 414385
I want to replace the date 2015-05-01 (from and to) with the current date
To replace the date, you could use string formatting. To post the request and to print the response into stdout:
#!/usr/bin/env python2
import urllib2
import sys
from datetime import date
from contextlib import closing
from shutil import copyfileobj
from urllib import quote
url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
data = "POSTDATA=FromDate={now}&ToDate={now}".format(now=quote(date.today()))
data += "&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
with closing(urllib2.urlopen(url, data)) as response:
copyfileobj(response, sys.stdout)
In general, consider urllib.urlencode()
, to create application/x-www-form-urlencoded
data instead of doing it manually.
Upvotes: 0
Reputation: 77912
I don't mean to sound harsh, but being new to a language is no excuse for not learning the language's syntax (quite on the contrary). This line:
data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
is obviously broken and raises a SyntaxError:
bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
>>> data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
File "<stdin>", line 1
data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
^
SyntaxError: invalid syntax
>>>
In this statement the rhs expression actually begins with :
"POSTDATA=FromDate="
which is a legal literal stringi&ToDate
which is parsed as "i
" (identifier) "&
" (operator) "ToDate
" (identifier)The mere juxtaposition of a literal string and an identifier (without an operator) is actually illegal:
bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
>>> i = 42
>>> "foo" i
File "<stdin>", line 1
"foo" i
^
SyntaxError: invalid syntax
>>>
Obviously what you want here is string concatenation, which is expressed by the add
("+
") operator, so it should read:
"POSTDATA=FromDate=" + i
Then since "&ToDate" is supposed to be a string literal instead of an operator and a variable you'd have to quote it:
"POSTDATA=FromDate=" + i + "&ToDate="
Then concatenate the current date again:
"POSTDATA=FromDate=" + i + "&ToDate=" + i + "etc..."
Now in your code i
(not how I would have named a date BTW but anyway) is a datetime
object, not a string, so now you'll get a TypeError
because you cannot concatenate a string with anything else than a string (hopefully - it wouldn't make any sense).
FWIW what you want here is not a datetime
object but the textual ("string") representation of the date in the "YYYY-MM-DD" format. You can get this from the datetime
object using it's strftime()
method:
today = datetime.datetime.now().strftime("%Y-%m-%d)
Now you have a string that you can concatenate:
data = "POSTDATA=FromDate=" + today + "&ToDate=" + today + "etc..."
This being said:
requests
being the de facto standard. Upvotes: 1
Reputation: 694
Forget urllib
and urllib2
libraries. I recommend you to use requests
library.
Here, solution of your problem using requests
library:
import requests, datetime
url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
i = datetime.datetime.now()
# add as many parameters as you like
payload = {'FromDate': i, 'ToDate': i, 'Pollutant': 'PM10', 'Format': 'XML'}
# here's the magic happens
r = requests.get(url, params=payload)
# status code of response
print r.status_code
# response text
print r.text
Upvotes: 0