requests 模块

import requests

head = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
}

# url = 'www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx'
# ur2 = 'https://www.baidu.com/s'
# ur3 = 'https://space.bilibili.com/291157863'
# ur4 = 'https://www.cnblogs.com/Anec/'
# ur5 = 'https://i.cnblogs.com/api/posts/list'
# ur6 = 'https://www.baidu.com'
#
# params = {
#     'w': 'python'
# }

# --------------------------------------------------
# 设置headers 参数
# 获取网页, headers 为dict类型
# 携带参数请求 params
# resp = requests.get(ur2, headers=head, params=params)

"""
resp = requests.get(ur6, headers=head)
get() 参数
    headers 请求头设置
    params 参数键值对设置
    timeout 超时时间设置
"""
# with open("test.html", 'wb') as f:
#     f.write(resp.content)


# --------------------------------------------------

# # 将返回的cookie 值字符串 转换为python dict 数据类型
# resp = requests.get(ur6, headers=head)
# cookieDict = requests.utils.dict_from_cookiejar(resp.cookies)
# print(cookieDict)

# --------------------------------------------------
# 编码问题

# 设置编码格式
# resp.encoding = 'utf-8'

# # 打印请求的网页内容 text 结果类型为 str 格式
# print(resp.text)

# # 打印编码格式
# print(resp.encoding)

# 获取结果时候转换编码格式,content 结果类型为 bytes 格式
# print(resp.content.decode("utf-8"))

# 解决中文乱码问题
# resp.content.decode() 默认为 UTF-8 的格式
# 常见编码字符集:
# GBK
# GB2312
# ASCII
# iso-8859-1

# --------------------------------------------------

# 请求的URL,str 类型
# print(resp.url)

# 状态码 ,int 类型
# print(resp.status_code)

# # 响应头, dict 类型
# print(resp.headers)

# # 相应对应请求的COOKIE,类型 为 RequestsCookieJar[]
# print(resp.request._cookies)

# # 相应的COOKIE,返回来的COOKIES值, RequestsCookieJar[]
# print(resp.cookies)

# # 若返回的数据是json 格式的,可以直接转为json数据
# print(resp.json())

# --------------------------------------------------
# 携带cookie 参数 的方式绕过登录获取数据  示例网站为博客园 后台页面
# 分析:后台数据是从接口获取的,需要登录传递cookie 参数,才能获取数据,否则获取不了用户的数据
# 请求的 url 地址为: https://i.cnblogs.com/api/posts/list?p=1&cid=&tid=&t=1&cfg=0&search=
# 返回的数据的为json数据
# {"postList":[
#               {"id":14244194,"title":"AD域(活动目录) bat脚本探究",


# --------------------------------------------------
# 代理
# 分类

# 透明代理
# 目标服务器接收到的请求如下:
# REMOTE_ADDR = PROXY IP
# HTTP_VIA = Proxy IP
# HTTP_X_FORWARDED_FOR = Your IP

# 匿名代理
# 目标服务器接收到的请求如下:
# REMOTE_ADDR = PROXY IP
# HTTP_VIA = Proxy IP
# HTTP_X_FORWARDED_FOR = Proxy IP


# 高匿代理
# 目标服务器接收到的请求如下:
# REMOTE_ADDR = PROXY IP
# HTTP_VIA = not determined
# HTTP_X_FORWARDED_FOR = not determined

# ---------------------

# 根据协议 的不同,需要使用相应协议的代理服务,从代理服务器请求使用的协议可以分为:

# http代理
# https 代理
# socket 隧道代理
#   只简单的传递数据包,不关心是何种应用协议(FTP,HTTP,HTTPS)
#   比http、https 代理耗时少
#   可以转发http和https 的请求

# 代理使用,成功则没有任何报错,能成功获取响应,失败的话,要么卡滞,要么就直接报错

"""
url = "http://www.baidu.com"
proxies = {
    'http': "http://112.95.21.191:8888"
}

# response = requests.get(url=url)
response = requests.get(url=url,proxies=proxies)
print(response.text)
"""
# --------------------------------------------------

# 忽略 CA 证书
"""
response = requests.get(url=url,verify=False)
"""

# --------------------------------------------------
# requests POST 请求

"""
data = {
}
response = requests.post(url=url,data=data)
"""

# POST 数据参数来源
# 1.固定值  抓包比较不变值
# 2. 输入值  抓包比较自身变化值
# 3.预设值 - 静态文件 Token
# 4.预设值-发请求 需要对指定接口发请求
# 5.在客户端生成的  分析js,模拟生成数据


# --------------------------------------------------

# requests.session()
# 怎么使用
"""
# 创建sesion 的类,然后请求页面
session = request.session()
response = session.get(url,data,hreaders)

"""

 1

posted @ 2021-02-19 23:04  Anec  阅读(8)  评论(0编辑  收藏  举报