【Python】python http 请求

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'])

猜你喜欢

转载自www.cnblogs.com/jzsg/p/10926193.html
今日推荐