【Python】python http 请求

python2.7 版

自定义设置代理、超时客户端

# -*- coding: utf-8 -*
# python2.7
import urllib2
import urllib
import json
import StringIO
import gzip
import sys
import socket
import logging
import random
import StringIO
import gzip

logging.basicConfig(
    level    = logging.DEBUG,
    format   = '%(asctime)s %(levelname)s %(process)d --- [%(threadName)s] %(filename)s %(funcName)s %(lineno)d : %(message)s',
    datefmt  = '%Y-%m-%d %H:%M:%S',
)


class JaguarClient(object):

    def __init__(self, proxy=None):
        handler = None
        if proxy is not None:
            handler = {"http":proxy}
        proxy_handler = urllib2.ProxyHandler(handler)
        self.__client = urllib2.build_opener(proxy_handler)


    def get(self, url, params=None):
        assert isinstance(url, str), "url must be str"
        if params is not None:
            assert isinstance(params, dict), "params must be dict"
            url = url + "?" + urllib.urlencode(params)
        response = None
        try:
            response = self.__client.open(url, timeout=10) # timeout 单位是秒
            if response.headers.get("content-encoding") == "gzip":
                gzipper = gzip.GzipFile(fileobj=StringIO.StringIO(response.read()))
                return gzipper.read()
            else:
                return response.read().decode("utf-8")
        except  IOError, err:
            if isinstance(err, socket.timeout):
                logging.error("Time Out | url: %s "%(url))
            else:
                logging.error("Requset | url: %s msg: %s"%(url, err))
            return None
        finally:
            if response is not None:
                response.close()

    def post_by_bodyIsJSON(self, url, header = {},params=None):
        assert isinstance(params, str), "params must be str"
        request = urllib2.Request(url, data=params, headers = header)
        response = self.__client.open(request, timeout=10)

        return response.code

def example():
    cli = JaguarClient()
    res_str = cli.get("http://www.sina.com")
    return res_str

def query_weater():
    city = ["杭州", "上海", "北京", "成都"]
    url = "http://wthrcdn.etouch.cn/weather_mini"
    cli = JaguarClient()
    res_str = cli.get(url, {"city":random.choice(city)})
    return res_str

if __name__ == "__main__":
    # res_str = example()
    res_str = query_weater()
    print res_str
# 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'])

“年轻时,我没受过多少系统教育,但什么书都读。读得最多的是诗,包括烂诗,我坚信烂诗早晚会让我邂逅好诗。” by. 马尔克斯
原文地址:https://www.cnblogs.com/jzsg/p/10926193.html