4

Actually, the question might be better worded as a request for a best practice for accomplishing this. It is frustrating because this should be easy.

I am following a tutorial in the book Flask by Example. I am using the latest version of python 3. The urllib2 used in the text cannot be found for python 3. From the text, we need urllib2 to download the data, and urllib to correctly encode the parameters. Just one function, get_weather because I cannot find an updated approach that works.

I will list the relevant lines of code to show what I am trying to accomplish. I am using the latest version of flask with python 3. I won't list the template file, as the problem did not show up until I tried to download a json api.

So, the first changes to the file, include imports for urllib and json. The book is from 2015 when urllib2 was available. We are trying to get the Weather from openwheathermap.org. Since I could not find urllib2, I modified the book's code slightly.

I have

WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather?q={}&APPID=myappid"

def get_weather(query):
  query = urllib.parse.quote(query)
  url = WEATHER_URL.format(query)
  data = urllib.request.urlopen(url).read()
  parsed = json.loads(str(data))
  weather = None
  if parsed.get('weather'):
    weather = {'description': parsed['weather'][0]['description'],
               'temperature': parsed['main']['temp'],
               'city': parsed['name'],
               'country': parsed['sys']['country']
               }
  return weather

Any advice would be appreciated.
Thanks, Bruce

Bruce Whealton
  • 1,041
  • 2
  • 14
  • 25
  • 2
    i think requests library is very popular. – Vasif Aug 23 '17 at 23:07
  • As stated in the urllib2 documentation: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3. – Fady Saad Aug 23 '17 at 23:15
  • What is this problem you are talking about? You can, of course, do this with the standard library `urllib` package. You just have to import the modules the right way. – Abdou Aug 24 '17 at 00:08
  • Where is duplicate? I had problem with urllib.urlopen(url). requests.get(url) works fine. If I want to get the data from a form and pass it to a get request: (1) should I use request.args.get(query) or request.form.get(query) – Bruce Whealton Aug 26 '17 at 19:38

1 Answers1

5

You can use requests library that has popularly cleaner interface to do http operations.

URL for the library. http://www.python-requests.org/en/master/

You can do

pip install requests

on your shell/cmd to install.

In code,

import requests
response = requests.get(WEATHER_URL.format(query))
weather = response.json() # or something as easy as this.
Vasif
  • 1,353
  • 10
  • 25
  • What does urllib.parse.quote() do? Do I need to escape any characters when getting data from a form before making the get request? – Bruce Whealton Aug 26 '17 at 19:15