learn_requests
# -*- 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