zoukankan      html  css  js  c++  java
  • Python-访问网络资源

    使用python写接口自动化脚本的时候,会使用到一系列请求来访问网络资源

    使用python内置模块

    from urllib import request
    
    
    def get_html(url):
        page = request.urlopen(url)
        html = page.read().decode('utf-8')   # 如果不用decode,获取的会是bytes
        return html

    使用python第三方模块

    import requests
    
    
    def get_status():
        resutls = requests.get("https://jt377.my.centrify.com/my?customerId=JT377")
        resutls_status_code = resutls.status_code
        print(resutls_status_code)   # 200
    
    
    def get_html():
        resutls = requests.get("https://jt377.my.centrify.com/my?customerId=JT377")
        resutls_html = resutls.text  # 通过文本的形式获取响应内容html
        resutls_html.encode(resutls.encoding).decode()  # 处理乱码
        print(resutls_html)    # <>
    
    
    def get_json():
        request_headers = {"X-CENTRIFY-NATIVE-CLIENT": "Web", "Content-Type": "application/json"}
        user_name_payload = {"AssociatedEntityName": "Portal", "AssociatedEntityType": "Portal",
                             "ExtIdpAuthChallengeState": "", "TenantId": "jt377", "User": "cat_test1@jt377",
                             "Version": "1.0"}
        resutls = requests.post("https://jt377.my.centrify.com/Security/StartAuthentication",
                             json=user_name_payload, headers=request_headers)
        resutls_json = resutls.json()  # 通过json形式获取响应内容
        print(resutls_json)   # {}
    
    
    def get_cookie():
        r = requests.Session()   # 自动获取cookie,并传入后面所有的请求中,经常用于web自动化让整个过程保持登录状态
        request_headers = {"X-CENTRIFY-NATIVE-CLIENT": "Web", "Content-Type": "application/json"}
        user_name_payload = {"AssociatedEntityName": "Portal", "AssociatedEntityType": "Portal",
                             "ExtIdpAuthChallengeState": "", "TenantId": "jt377", "User": "cat_test1@jt377",
                             "Version": "1.0"}
        resutls = r.post("https://jt377.my.centrify.com/Security/StartAuthentication",
                                json=user_name_payload, headers=request_headers)
        headers = r.headers
        print(headers)
  • 相关阅读:
    Jdbc、Mybatis、Hibernate各自优缺点及区别
    java中的线程池原理
    JVN的理解
    "=="和 equals 方法究竟有什么区别?
    nginx基础之【study one】
    C#,WPF中使用多文本显示数据,并对其数据进行关键字高亮等操作
    C#、WPF中如何自定义鼠标样式
    SSM(Spring + Springmvc + Mybatis)框架面试题
    java基础面试题
    C#关于TreeView树在节点数较多时总是会出现闪烁的问题方法记录
  • 原文地址:https://www.cnblogs.com/lj8023wh/p/10605329.html
Copyright © 2011-2022 走看看