码迷,mamicode.com
首页 > 编程语言 > 详细

【Python】python http 请求

时间:2019-05-26 16:07:43      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:load   please   城市   pytho   python   read   pre   _for   htm   

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 请求

标签:load   please   城市   pytho   python   read   pre   _for   htm   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!