0

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
1
  • So what exactly is the problem / question? Commented Sep 29, 2015 at 10:24

3 Answers 3

1

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 string
  • i&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:

  • this kind of operation is usually done using string formatting
  • and as Ekrem Dogan mentionned, the simplest solution here is to use a higher-level package that will take care of all the boring details of HTTP requests - requests being the de facto standard.
Sign up to request clarification or add additional context in comments.

Comments

0

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 

3 Comments

I just run this script, and got an xml response about 55MB. You may want to narrow your search with additional parameters.
thank you very much Ekrem!Indeed the response xml is big,i may filter them based on the users location(from the android application i am developing) !
@mikaeloN glad to help! Hope this was the answer to your question.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.