requests模块
response = requests.get("www.baidu.com") //用get的传参获取网页请求
response.text //获取网页html html类型 输出解码
response.content //类型是bytes
encoding = response.encoding //获取编码方式
解码
response.content.decode() //默认用utf-8解码
response.content.decode("rgb")
发送带有headers头的请求
//获取一个user-agent头
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"}
//发送带有headers头的get请求
response = requests.get("http://www.baidu.com",headers=headers)
//将请求结果 以utf-8解码后显示
html = response.content.decode()
发送带有参数的请求
params传参
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"}
p = {"wd":"陈嘉泽"}
url = "http://www.baidu.com"
r = requests.get(url,headers=headers,params=p)
format传参
url = "http://www.baidu.com/s?wd={}".format("4399")
r = requests.get(url,headers=headers)
使用代理
proxies = {"http":"http://175.43.56.60:9999"}
response = requests.get("http://www.baidu.com",proxies=proxies,headers=headers)
处理cookie、session请求
session = requests.session() //实例化一个session对象
response = session.get(url,headers) //让session发送get/post请求