requests入门大全
阅读原文时间:2023年07月09日阅读:2

安装常见问题

  • 提示连接不上,443问题

一般是因为浏览器设置了代理,关闭代理。

  • 网络加载慢,设置国内镜像地址

1.pip安装

2.pycharm安装

  • 国内镜像源

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

  • 临时使用

在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple

例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider,这样就会从清华镜像去安装pyspider库。

参考:https://www.cnblogs.com/cqliu/p/11131092.html

  • requests工作原理

  • 代码解析

通过追踪代码,可以发现requests实际上是调用了request方法,源码如下:

method, url是必填参数,其他是可选参数。

参数method,有GET, OPTIONS, HEAD, POST, PUT, PATCH, DELETE

可选参数可以发送data、json、headers、cookies、flies、auth、timeout、proxies、verify、stream、cert

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
    :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
        defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
        to add for the file.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use. Defaults to ``True``.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response

    Usage::

      >>> import requests
      >>> req = requests.request('GET', 'https://httpbin.org/get')
      >>> req
      <Response [200]>
    """

    # By using the 'with' statement we are sure the session is closed, thus we
    # avoid leaving sockets open which can trigger a ResourceWarning in some
    # cases, and look like a memory leak in others.
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)
  • 请求参数保存在url中,?后面添加参数名称和参数值,如果有多个参数,使用&连接

  • 请求参数保存在params中,params以字典形式保存数据

接口说明:

淘宝网

API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860

参数:

tel:手机号码

返回:JSON

import requests
def test_get():
    # 参数保存在url中,?后面添加参数名称和参数值,如果有多个参数,使用&连接
    url = 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
    r=requests.get(url=url)
    print(r.text)
    print(r.status_code)
    print(r.cookies)
    print(r.url)
    # 参数保存在params中,params以字典形式保存数据
    url = 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm'
    params={'tel':13909161860}
    r=requests.get(url=url,params=params)
    print(r.text)
    print(r.status_code)
    print(r.cookies)
    print(r.url)


import requests

def test_requests():
    r = requests.get('https://www.baidu.com')
    print('\n')
    print('状态码是:', r.status_code)
    print('url是:', r.url)
    print('头信息是:', r.headers)
    print('cookies是:', r.cookies)
    print('text是:', r.text)
    print('请求是:', r.request)
  • post发送form格式请求参数,使用data

  • post发送json格式请求参数,使用json

  • 什么是cookies?

相当于临时身份证。

  • 谷歌浏览器抓包

查看请求参数的格式,看Content-Type字段

如:Content-Type: text/html;charset=utf8,表示请求参数是text格式,编码是utf-8

如果是form式,可以查看form data具体发送的参数

在脚本中发送

查看cookies

  • 解码

编码:把码位转换成字节序列(通俗来说:把字符串转换成用于存储或传输的字节序列,python中是.encode())

解码:把字节序列转换成码位(通俗来说:把字节序列转换成人类可读的文本字符串,python中是.decode())

headers参数不是每个接口都需要添加的,开发可以自行定义。

  • 不需要添加headers

    import requests
    def test_51job():
    # 前程无忧搜索岗位不需要headers信息
    url='https://search.51job.com/list/030200,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E6%25B5%258B%25E8%25AF%2595%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare='
    r_51job=requests.get(url=url)
    # print(r_51job.text)
    print(r_51job.encoding) # html默认的编码方式是ISO-8859-1
    r_51job.encoding='gb2312' # 修改编码方式
    print(r_51job.text)

  • 乱码问题

  • 需要添加headers

    import requests
    def test_12306():
    # 12306搜索车票需要添加headers信息,只需要cookies即可。实际工作中,headers需要哪些值,咨询开发人员了解。
    url='https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2020-06-06&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=GZQ&purpose_codes=ADULT'
    headers={'Cookie': 'JSESSIONID=C5DCA622147CEF2C4D9C3D3E53E9CF26; BIGipServerotn=1406730506.64545.0000; RAIL_EXPIRATION=1591740743186; RAIL_DEVICEID=FqtrEO_1lQCMdLmY_0uxjBNyLf5esH-V-KtA_I-kPu0j721_HYTxo4IobbdtZbAr75fLtGhLHAQTE8mWEaNmM7ococf3hUVIFw-Gaaper7CzwlrDFsHwwey8w_YQ5gGoMoyfKgVJ5o4nNuWVYhuiC_cxPzsWFJkF; BIGipServerpassport=954728714.50215.0000; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_toStation=%u5E7F%u5DDE%2CGZQ; _jc_save_fromDate=2020-06-06; _jc_save_toDate=2020-06-06; _jc_save_wfdc_flag=dc; _jc_save_fromStation=%u4E0A%u6D77%2CSHH'}
    r_12306=requests.get(url=url,headers=headers)
    print(r_12306.text)

  • 使用chrome查看headers

https协议需要证书

解决方法

  • 发送请求时忽略证书,证书参数verify

直接访问,报错。(我自己尝试,没有添加verify尝试,也没有出现错误)

verify参数默认为True,设置为False表示忽略证书

import requests
def test_verify():
    url='https://www.ctrip.com/'
    r=requests.get(url=url,verify=False)
    print(r.text)
  • 在verify里面添加证书路径

  • 携程上传头像

    import requests

    def test_files():
    url='https://sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx' # 携程上传头像
    cookies={'cookie': 'cookies的值'}
    files={'uploadFile_852':('Markdown.jpg',open('D:\Markdown.jpg','rb'),'image/jpeg')}
    r=requests.post(url=url,files=files,verify=False,cookies=cookies)
    print(r.text)

files字段说明:

uploadFile_852 文件参数的名字,通过抓包工具查看,实际工作中直接向开发咨询了解。

Markdown.jpg 文件名

open('D:\Markdown.jpg','rb') 打开本地一个文件,注意使用\转义

image/jpeg 文件格式

  • 禅道上传csv文件

上传文件,你一般需要什么参数?

1.url

2.请求参数

3.请求方式

4.files参数

5.cookies

  • 接口测试两个检查点

1.校验返回结果与开发定义的(接口文档)是否一致,如上传成功返回1,上传失败返回0

2.校验接口逻辑是否正确,如上传文件后会把文件保存在服务器的一个目录下,到该目录下检查是否存在上传的文件

  • 超时测试介绍

  • 超时异常

    requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='tcc.taobao.com', port=80): Max retries exceeded with url: /cc/json/mobile_tel_segment.htm?tel=13909161860 (Caused by ConnectTimeoutError(, 'Connection to tcc.taobao.com timed out. (connect timeout=0.01)'))

import requests

def test_timeout():
    url = 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13909161860'
    r=requests.get(url=url,timeout=1)
    print(r.text)
  • 轮循

    import requests

    def test_files():
    # 携程上传用户头像
    for i in range(0,10):
    try:
    url='https://sinfo.ctrip.com/MyInfo/Ajax/UploadPhoto.ashx'
    cookies={'cookie': 'cookies的值')}
    r=requests.post(url=url,files=files,verify=False,cookies=cookies,timeout=0.25)
    print(r.text)
    except:
    print('运行出错')

  • 导出禅道用例模板

  • 思路

  • stream参数的使用

  • 为什么要使用session参数?

有很多接口需要一些公共的参数,比如cookies,可节省时间,精简代码

def test_seesion():
    s=Session()
    s.headers={'h':'test_headers'}
    r=s.post('https://httpbin.testing-studio.com/post',data={'a':1}) # 就不需要再单独添加headers
    print(r.json())

token放在data参数里

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3

  • 微信支付签名算法

(签名校验工具)

签名生成的通用步骤如下:

第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。

特别注意以下重要规则:

◆ 参数名ASCII码从小到大排序(字典序);

◆ 如果参数的值为空不参与签名;

◆ 参数名区分大小写;

◆ 验证调用返回或微信主动通知签名时,传送的sign参数不参与签名,将生成的签名与该sign值作校验。

◆ 微信接口可能增加字段,验证签名时必须支持增加的扩展字段

第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。

◆ key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置

举例:

假设传送的参数如下:

appid: wxd930ea5d5a258f4f

mch_id: 10000100

device_info: 1000

body: test

nonce_str: ibuaiVcKdpRxkhJA

第一步:对参数按照key=value的格式,并按照参数名ASCII字典序排序如下:

stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";

第二步:拼接API密钥:

stringSignTemp=stringA+"&key=192006250b4c09247ec02edce69f6a2d" //注:key为商户平台设置的密钥key

sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7" //注:MD5签名方式

sign=hash_hmac("sha256",stringSignTemp,key).toUpperCase()="6A9AE1657590FD6257D693A078E1C3E4BB6BA4DC30B23E0EE2496E54170DACD6" //注:HMAC-SHA256签名方式,部分语言的hmac方法生成结果二进制结果,需要调对应函数转化为十六进制字符串。

最终得到最终发送的数据:

<xml>

<appid>wxd930ea5d5a258f4f</appid>

<mch_id>10000100</mch_id>

<device_info>1000</device_info>

<body>test</body>

<nonce_str>ibuaiVcKdpRxkhJA</nonce_str>

<sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>

</xml>

简单来说,就是通过上面的规则生成sign签名,然后放在sign参数发送。


proxies = {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'}
def test_proxies():
    r = requests.get("https://httpbin.testing-studio.com/get",
                     # get请求带query参数 ,为 URL 的查询字符串(query string)传递某种数据
                     params={
                         'a': 1,
                         'b': 2,
                         'c': 'asd'
                     },
                     proxies=proxies,
                     verify=False  # 关闭校验,否则代理无法抓包
                     )

    print(r.json())

hook机制可修改响应返回的内容,或替换响应返回的内容。

import requests

def test_get_hook():
    def hook(r,*args,**kwargs):
        r.demo = 'demo content'
    r = requests.get("https://httpbin.testing-studio.com/get",
                     params={
                         'a': 1,
                         'b': 2,
                         'c': 'asd'
                     },
                     hooks={'response': [hook]}
                     )
    print(r.json())
    print(r.demo)
  • 尽量减少后期的维护成本

1.测试数据的维护

2.测试脚本的维护

  • 尽量减少测试用例之间的依赖性

  • 覆盖的全面性

  • 易理解易阅读

规范命名

添加注释

  • 最重要的一点是做自动化测试要基于公司的需求,从公司需求出发。

通过配置文件实现。

在实际工作中,可能需要在不同的测试环境测试,此时可以在配置环境定义好不同的环境,通过更改配置,达到在不同环境进行测试的目的。

配置文件

def server_ip():
    # 配置文件,通过修改配置,在不同环境进行测试
    dev_ip='https://www.baidu.com/'
    sit_ip='https://cn.bing.com/'
    return sit_ip

def sql_conf():
    # 定义数据库的配置
    ip=''
    username=''
    password=''
    port=3306
    charset='utf-8'
    return ip,username,password,port,charset


import requests
from config.conf import * # 从config导入

def test_conf():
    url=server_ip()+'/login'  # server_ip()表示引入这个函数,而不是这个方法
    print(url)
    r=requests.get(url=url)
    print(r.text)
import requests
from config.conf import * # 从config导入

def test_conf():
    url=server_ip()+'/login'  #
    print(url)
    r=requests.get(url=url)
    print(r.text)

19 requests接口自动化-excel参数化

20 requests接口自动化-数据库参数化

21 requests接口自动化-列表与字典参数化

22 requests接口自动化-动态关联text/html格式

23 requests接口自动化-动态关联json格式

24 requests接口自动化-assert断言

25 pytest框架-安装

26 pytest框架-运行测试用例

27 pytest框架-动态关联

28 pytest框架-生成测试报告

29 Jenkins+requests+pytest+allure持续集成