标签:进制 二进制 mac os response span print web 表示 stat
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多
各种请求方式,最好加上头信息和异常判断
import requests requests.get(‘http://httpbin.org/get‘) requests.post(‘http://httpbin.org/post‘) requests.put(‘http://httpbin.org/put‘) requests.delete(‘http://httpbin.org/delete‘) requests.head(‘http://httpbin.org/get‘) requests.options(‘http://httpbin.org/get‘)
url参数
1.直接放在url
import requests response = requests.get(http://httpbin.org/get?name=gemey&age=22) print(response.text)
2.放在一个字典中
import requests data = { ‘name‘: ‘tom‘, ‘age‘: 20 } response = requests.get(‘http://httpbin.org/get‘, params=data) print(response.text)
添加头信息
import requests heads = {} heads[‘User-Agent‘] = ‘Mozilla/5.0 ‘ ‘(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ‘ ‘(KHTML, like Gecko) Version/5.1 Safari/534.50‘ response = requests.get(‘http://www.baidu.com‘,headers=headers)
r.status_code
http请求的返回状态,200表示连接成功,404表示连接失败
r.text
http响应内容的字符串形式,url对应的页面内容
r.encoding
从HTTP header中猜测的响应内容编码方式
r.apparent_encoding
从内容分析出的响应内容的编码方式(备选编码方式)
r.content
HTTP响应内容的二进制形式
r.headers
http响应内容的头部内容
r..cookies
获取cookie
r.Session
会话维持
import requests response = requests.get(‘http://httpbin.org/get‘) print(response.text) print(response.json()) #response.json()方法同json.loads(response.text) print(type(response.json()))
import requests response = requests.get(‘http://img.ivsky.com/img/tupian/pre/201708/30/kekeersitao-002.jpg‘) b = response.content with open(‘F://fengjing.jpg‘,‘wb‘) as f: f.write(b)
标签:进制 二进制 mac os response span print web 表示 stat
原文地址:https://www.cnblogs.com/dangjf/p/10174320.html