zoukankan      html  css  js  c++  java
  • python【网络相关】【requests】

    1. 基础知识
    # 1. response的属性
    import requests
    response=requests.get("http://www.baidu.com/")
    print(response)            #  <Response [200]>
    print(type(response))   # <class 'requests.models.Response'>
    
    
    
    #  2. response.status_code
    # http请求的返回状态,2XX 表示连接成功,3XX 表示跳转 , 4XX 客户端错误 , 500 服务器错误
    
    
    
    # 3. response.text
    # http响应内容的 字符串(str) 形式,请求url对应的页面内容
    response=requests.get("http://www.baidu.com/")
    print(response.text) # 打印出的内容含有乱码: <title>ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“</title>
    # 修改如下, 改变下载得到的页面的编码,就可以正常打印出"友好的"文本了
    response.encoding="utf-8"
    print(response.text)  # 打印文本中没有乱码,更改编码使用:response.encoding=“utf-8” 或者 response.encoding=”gbk” 具体要看你请求的网页是用什么方式编码的,针对不同情况用对应的编码方式
    
    
    
    # 4. response.content
    # HTTP响应内容的 二进制(bytes) 形式
    response =requests.get("http://10.101.35.249:5000/")
    print(response.content)                  #打印出的是二进制形式, # b'
            <html>
                <body>
                    <h1>Reset time</h1>
    
    print(response.content.decode("utf-8"))  #打印出的是网页的文本,和response.text效果一样
    '''
            <html>
                <body>
                    <h1>Reset time</h1>
                    <div><table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>PTP_sync</th>
          <th>path_sync</th>
          <th>Tx_setup</th>
    '''
    
    
    
    # 5. response.encoding
    # 从HTTP header中猜测的响应内容编码方式
    
    
    
    # 6. response.headers
    #http响应内容的头部内容
    
  • 相关阅读:
    【做题】提高组过关测试题1
    【做题】arc078_f-Mole and Abandoned Mine——状压dp
    【学习】Hall’s Marriage Theorem
    【做题】arc072_f-Dam——维护下凸包
    一文尽览近似最近邻搜索中的哈希与量化方法
    InsightFace源码以及pre-train模型以及使用
    无法解析的外部符号 jpeg_std_error
    威布尔weibull distribution
    中科院- 生物特征识别概述
    Face-Resources
  • 原文地址:https://www.cnblogs.com/amize/p/15093367.html
Copyright © 2011-2022 走看看