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响应内容的头部内容
    
  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/amize/p/15093367.html
Copyright © 2011-2022 走看看