相关疑难解决方法(0)

什么python模块替换urllib2用于python 3和烧瓶?

实际上,这个问题的措辞可能更好,因为要求实现这一目标的最佳实践.这很令人沮丧,因为这应该很容易.

我正在遵循Flask by Example一书中的教程.我使用的是python 3的最新版本.在python 3中找不到文本中使用的urllib2.从文本中,我们需要urllib2来下载数据,并使用urllib来正确编码参数.只有一个函数,get_weather因为我找不到有效的更新方法.

我将列出相关的代码行,以显示我想要完成的任务.我使用python 3的最新版本的烧瓶.我不会列出模板文件,因为直到我尝试下载json api时才出现问题.

因此,对文件的第一个更改包括urllib和json的导入.这本书是从2015年开始的,当时urllib2可用.我们正试图从openwheathermap.org获取天气.由于我找不到urllib2,我稍微修改了本书的代码.

我有

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
Run Code Online (Sandbox Code Playgroud)

任何意见,将不胜感激.
谢谢,布鲁斯

python json urllib

4
推荐指数
1
解决办法
731
查看次数

尝试从request.json获取值时,"dict对象不可调用"

我使用JavaScript获取用户位置并将经度和经度发送到Flask应用程序.但是,TypeError: 'dict' object is not callable当我试图获得纬度时,我得到了request.json.为什么我会收到此错误,如何解决?

@app.route('/location', methods = ['POST'])
def location():
    latitude = request.json('latitude')
    longitude = request.json('longitude')
    send_email("myemail@example.com","Location:",str(latitude) + str(longitude))
Run Code Online (Sandbox Code Playgroud)
function showPosition(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    $.ajax({
    type: 'POST',
    url: '/location',
    data: JSON.stringify({latitude: latitude, longitude: longitude}),
    contentType: 'application/json;charset=UTF-8'
    });
}
Run Code Online (Sandbox Code Playgroud)

python json flask

1
推荐指数
1
解决办法
8086
查看次数

将response.text转换为python中的字典

我有一个 API,它在response.text 中为我提供以下数据,如何将这些数据转换为 python 字典?

响应.文本

[{"_id":"5dccedadff47e867a2833819","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:19.664Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:17.166Z"},{"_id":"5dccedacff47e867a2833801","tel":"XXXX","loc":[28.498692,77.095215],"tripId":"5dccedaaff47e867a28337ec","mode":"automated","osm_data":{"distance_remained":10791,"time_remained":1649.5},"distance_remained":10870,"time_remained":1173,"curr_ETA":"2019-11-14T06:43:18.459Z","address":"100,
The National Media Centre, Sector 24, Gurugram, Haryana 122022,
India","city":"Gurugram","createdAt":"2019-11-14T06:01:16.163Z"}]
Run Code Online (Sandbox Code Playgroud)

我想以字典的形式访问此response.text中的数据

python django

-2
推荐指数
1
解决办法
9683
查看次数

标签 统计

python ×3

json ×2

django ×1

flask ×1

urllib ×1