首页 > 编程语言 > 详细

【Python】python http 请求

时间:2019-05-26 15:44:25      阅读:111      评论:0      收藏:0      [点我收藏+]

python2.7 版

# coding=UTF-8
# urllib2_get.py

import urllib2
import StringIO
import gzip

url = 'http://wthrcdn.etouch.cn/weather_mini?city=杭州'

response = urllib2.urlopen(url).read()

data = StringIO.StringIO(response)
gzipper = gzip.GzipFile(fileobj=data)
html = gzipper.read()

print
html

python3.6 版

import json
import requests
import sys

if len(sys.argv) < 2:
    print('Please city params !')
    sys.exit()

# 从命令行获取城市 索引1
city = sys.argv[1]

# 请求天气数据
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + city
response = requests.get(url)
response.raise_for_status()  # 用来检查错误
weatherData = json.loads(response.text)  # 返回的是json数据
print(weatherData['data'])

【Python】python http 请求

原文:https://www.cnblogs.com/jzsg/p/10926193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!