1,导入requests模块
==============================================================
get请求:
url = host = 'https://www.baidu.com/s'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'}
data = {'wd':'猫'}
response = requests.get(url=url,headers=headers,params=data,json=json,data=data)
print(response.content.decode("utf-8"))
get请求中的参数可以在URL中,也可以在字典中
===========================================================================
响应的解码方法有几种方式:
第一种方法:
response.encoding = "utf-8" # 类型:str 解码:自动根据响应头进行推测
print(response.text) # 直接text打印是乱码,因为text默认返回的是Unicode格式
第二种方法:
print(response.content.decode("utf-8")) # content是二进制模式,通过指定utf-8格式输出
第三种方法:
response.encoding = response.apparent_encoding # 解码:系统自动推测 如果不知道是什么编码格式,就使用这个方法
print(response.text)
获取基本信息:
print("状态码:",response.status_code)
print("请求URL:",response.url)
print("头部信息:",response.headers)
print("cookie信息:",response.cookies)
==========================================================================
post请求:
tag_url = 'https://api.weixin.qq.com/cgi-bin/tags/create'
tag_data = {'access_token':token}
tag_json_body = {"tag":{"name" :"长沙002"} }
headers = {"content-type":"application/json"} # 发送json 数据必须带有头部信息 content-type
response = requests.post(url=tag_url,params=tag_data,headers=headers,data=json.dumps(tag_json_body))
print(response.content.decode("utf-8"))
发送post请求的基础知识 dumps和loads
str1 = json.dumps(str_dict) # json.dumps 是把字典、json对象转成字符串
str_json = json.loads(str2) # json.loads 是把字符串转成json对象
注:response一般返回的数据都为字符串格式 /
也可通过json_obj=response.json()的方式转成json格式,可以通过json_obj["key"]提取json中key的值
# 发送json 数据必须带有头部信息 content-type :headers = {"content-type":"application/json"}
=====================================================================
2,导入json模块:
通过json把数据转成字符串或者json对象
1,json.dumps 是把字典、json对象转换为字符串
2,json.loads 是将字符串转成字典,json对象
=====================================================================
3,上传文件:
wx_url = "https://api.weixin.qq.com/cgi-bin/media/upload"
wx_data = {"access_token":token,"type":"image"}
file = {"files":open("E:/12345678.png","rb")} # 注意:必须要用字典的方式open
res = requests.post(url=wx_url,params=wx_data,files=file)
print(res.content.decode("utf-8"))
========================================================================
4,设置代理
# 设置代理,多用于爬虫
proxies = {"http":"http://12.34.56.79:9527",
"https":"https://12.34.56.79:9527"}
# 1,普通的代理
# res = requests.get(url="http://www.hnxmxit.com",proxies=proxies)
# print(res.content.decode("utf-8"))
# 2,携带了登录的用户名和密码
proxies = {"http":"http://用户名:密码@12.34.56.79:9527"}
res = requests.get(url="http://hmxmxit.com",proxies=proxies)
print(res.content.decode("utf-8"))
========================================================================
5,连接超时
# 2,链接超时,接收超时
print(time.time())
res = requests.get(url="http://www.hnxmxit.com",timeout=(0.01,2)) # 以秒为单位(连接,接收)
print(time.time())
==========================================================================
6,重定向
res = requests.get(url="http://www.360buy.com",allow_redirects=False) # 设置重定向开关
print(res.history) # 查看重定向历史
print(res.url)
==========================================================================
7,添加cookie
1,可以在参数中添加cookie
2,在header中添加cookie
# 1,在参数中添加
# cookie_dict = {"login_name":"admin"}
# res = requests.get(url="http://www.hnxmxit.com",cookies=cookie_dict)
# 2,在头部添加
cookie_header = {"login_name":"root"}
res = requests.get(url="http://www.hnxmxit.com",headers=cookie_header)
==========================================================================
8,关闭证书:
# 关闭警告
# requests.packages.urllib3.disable_warnings()
# warnings.filterwarnings("ignore")
# 1,关闭证书
# res = requests.get(url="https://www.12306.cn",verify=False)
# print(res.content.decode("utf-8"))
# 2,安装pyopenssl模块 可以验证证书,可以在请求中不加 verify=False
res = requests.get(url="https://www.12306.cn")
print(res.content.decode("utf-8"))
# 3,加上证书,找公司开发人员要 .crt文件 推荐这种
res = requests.get(url="https://www.12306.cn",cert=("/路径/证书.crt","/路径/key"))
==============================================================================
9,请求异常处理
ReadTimeout (加载超时)
ConnectionError(链接超时)
RequestException (request错误)
导入包
import requests
from requests.exceptions import ReadTimeout,ConnectionError,RequestException
try:
res = requests.get(url="http://www.hnxmxit.com",timeout=(0.01,1))
print(res.status_code)
except ReadTimeout as e:
print("加载超时")
print(e)
except ConnectionError:
print("链接超时")
except RequestException as e:
print(e)
=============================================================================
10,re模块
# 写法1:用re的函数进行匹配,会取第一个
str1 = "python3helloworldpython4"
value = re.search("python\d",str1).group()
print(value)
# 写法2:用正则表达式对象
reg_obj = re.compile("python\d") # 创建正则对象
value2 = reg_obj.search(str1).group()
print(value2)
title_str = "<title>新梦想培训_软件测试培训_Java培训班_IT技术培训_软件测试视频课程_IT培训机构</title>"
reg_obj2 = re.compile("t+")
value3 = reg_obj2.findall(title_str)[0] # findall 返回的是一个列表
print(value3)
value4 = re.findall("<title>(.+?)</title>",title_str)[0]
print(value4)
=======================================================================
=======================================================================
11,设置 session 会话保持连接
session_obj=requests.session() # 用来保持会话连接,后面的请求都用session对象来发送
# session添加cookie
# 方法2: 推荐使用
# session_obj.cookies.set("test_add_cookie","newdream")
# response = session_obj.get(url="http://www.hnxmxit.com")
13,添加cookie绕过登录phpwind论坛
# 1.手动打开论坛获取登录后的cookie (在F12下找Application的cookies)
# 2.添加cookie
session_obj = requests.session()
session_obj.cookies.set("zFb_lastvisit","142%091621076949",domain="47.107.178.45",path="/")
session_obj.cookies.set("zFb_winduser","WNirQHIeouFINxWV",domain="47.107.178.45",path="/")
=================================================================================
12,封装get/post请求,封装测试类
class run_test:
def __init__(self)
def send_get()
def send_post()