zoukankan      html  css  js  c++  java
  • 【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. 马尔克斯
  • 相关阅读:
    JSP中 == 和equals的区别
    使用Cookie保存用户名密码,再次登陆时将Cookie用户名密码取出来并直接放置到用户名密码文本框中
    学习Java Web开发中遇到的问题,及其解决方法
    部署、测试、服务工作的经验记录
    Python基础--dict字典操作
    Python基础--dict字典
    Python基础--预留空 5
    Python基础--预留空 4
    Python基础--tuple 元组
    Python基础--预留3
  • 原文地址:https://www.cnblogs.com/jzsg/p/10926193.html
Copyright © 2011-2022 走看看