码迷,mamicode.com
首页 > 其他好文 > 详细

learn_requests

时间:2017-09-29 23:05:05      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:imp   requests   json   response   cin   agent   写入   ict   cookie   

# -*- coding: utf-8 -*-
import requests

response = requests.get(https://api.github.com)
print "状态码,具体解释"
print response.status_code, response.reason
print "头部信息"
print response.headers
print "URL 信息"
print response.url
print "redirect 信息"
print response.history
print "耗费时长"
print response.elapsed
print "request 信息"
print response.request.method

print ----------------------

print "编码信息"
print response.encoding
print "消息主体内容: byte"
print response.content, type(response.content)
print "消息主体内容: 解析"
print response.text, type(response.text)
print "消息主体内容"
print response.json(), type(response.json())

 

# -*- coding: utf-8 -*-
import requests


def get_key_info(response, *args, **kwargs):
    """回调函数
    """
    print response.headers[Content-Type]


def main():
    """主程序
    """
    requests.get(https://api.github.com, hooks=dict(response=get_key_info))

main()

 

# -*- coding: utf -*-
import requests


def download_image():
    """demo: 下载图片, 文件
    """
    headers = {User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36}
    url = "http://img3.imgtn.bdimg.com/it/u=2228635891,3833788938&fm=21&gp=0.jpg"
    response = requests.get(url, headers=headers, stream=True)
    with open(demo.jpg, wb) as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)


def download_image_improved():
    """demo: 下载图片
    """
    # 伪造headers信息
    headers = {User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36}
    # 限定url
    url = "http://img3.imgtn.bdimg.com/it/u=2228635891,3833788938&fm=21&gp=0.jpg"
    response = requests.get(url, headers=headers, stream=True)
    from contextlib import closing
    with closing(requests.get(url, headers=headers, stream=True)) as response:
        # 打开文件
        with open(demo1.jpg, wb) as fd:
            # 每128写入一次
            for chunk in response.iter_content(128):
                fd.write(chunk)

download_image_improved()

 

 

session和cookies

技术分享

 

技术分享

 

learn_requests

标签:imp   requests   json   response   cin   agent   写入   ict   cookie   

原文地址:http://www.cnblogs.com/coder-gao/p/7612694.html

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