• requests 模块笔记


    import requests
    
    请求方式:
    
        requests.get("https://www.baidu.com")
        requests.post("http://httpbin.org/post")
        requests.put("http://httpbin.org/put")
        requests.delete("http://httpbin.org/delete")
        requests.head("http://httpbin.org/get")
        requests.options("http://httpbin.org/get")
    
    获取数据:
        
        r = requests.get('http://www.baidu.com') #像目标url地址发送get请求,返回一个response对象
        
        response.text返回的是Unicode格式,通常需要转换为utf-8格式。
        response.content是二进制模式,可以下载视频之类的,如果想看的话需要decode成utf-8格式。
        # response.content.decode("utf-8) 或 response.encoding="utf-8" 转码
        
        print(r.text)            # 返回响应的内容
      print(r.content)        # 这样获取的数据是二进制数据
        print(r.url)            # 打印请求网址的地址
        print(r.status_code)    # 打印请求页面的状态(状态码)# r.ok的布尔值便可以知道是否登陆成功
        print(r.cookies)        # 打印请求网址的cookies信息
        print(r.headers)        # 打印请求网址的headers所有信息
        print(r.encoding)        # 获取/修改网页编码
        print(r.json())            # 返回json数据
        
        print(r.history)        # 打印请求的历史记录(以列表的形式显示)
    
    # 下载图片
        response = requests.get("https://github.com/favicon.ico")
        with open('favicon.ico', 'wb') as f:
            f.write(response.content)
    
    
    例子:往请求链接中添加一些数据(data、headers、cookies、proxies...):
        
        import requests
        
        data = {'name': 'germey', 'age': '22'}
        cookie = {'key':'value'}
        headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
        proxies = {"http": "http://127.0.0.1:9743}
        
        requests.get(url='https://www.baidu.com/baidu', params={'wd': 'python',})
        # 为url传递参数 https://www.baidu.com/baidu?wd=python     
    
        response = requests.post(
            "https://www.zhihu.com/explore",
            data=data,  # 如果传递的是string而不是dict,需要先调用dumps方法格式化一下
            cookies=cookie,
            headers=headers, 
            proxies=proxies)
        print (response.headers.get('content-type')) #访问响应头部分内容
    
    补:
        requests.get(url, allow_redirects=False)    # 不允许重定向,默认True
        # verify=False    # 关闭证书验证
    其他操作 会话维持 cookie的一个作用就是可以用于模拟登陆,做会话维持 import requests session = requests.session() session.get('http://httpbin.org/cookies/set/number/12456') response = session.get('http://httpbin.org/cookies') print(response.text) 代理设置 import requests proxies = { "http": "http://127.0.0.1:9743", # 设置普通代理 "https": "https://127.0.0.1:9743", # 设置普通代理 "http": "http://user:password@127.0.0.1:9743/", # 设置用户名和密码代理 } response = requests.get("https://www.taobao.com", proxies=proxies) print(response.status_code) 设置socks代理 安装socks模块 pip3 install 'requests[socks]' import requests proxies = { 'http': 'socks5://127.0.0.1:9742', 'https': 'socks5://127.0.0.1:9742' } response = requests.get("https://www.taobao.com", proxies=proxies) print(response.status_code) 超时设置 import requests from requests.exceptions import ReadTimeout try: response = requests.get("http://httpbin.org/get", timeout = 0.5)#设置秒数超时,仅对于连接有效 print(response.status_code) except ReadTimeout: print('Timeout') 获取cookie import requests response = requests.get("https://www.baidu.com") print(response.cookies) for key, value in response.cookies.items(): print(key + '=' + value) 文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("http://httpbin.org/post", files=files) print(response.text) 认证设置 import requests from requests.auth import HTTPBasicAuth r = requests.get('http://120.27.34.24:9001', auth=HTTPBasicAuth('user', '123')) # r = requests.get('http://120.27.34.24:9001', auth=('user', '123')) print(r.status_code) 关闭证书验证 import requests response = requests.get('https://www.12306.cn',verify=False) print(response.status_code) 消除验证证书的警报 from requests.packages import urllib3 import requests urllib3.disable_warnings() response = requests.get('https://www.12306.cn',verify=False) print(response.status_code) 手动设置证书 import requests response = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key')) print(response.status_code) 异常处理 # RequestException继承IOError,    # HTTPError,ConnectionError,Timeout继承RequestionException    # ProxyError,SSLError继承ConnectionError    # ReadTimeout继承Timeout异常 import requests from requests.exceptions import ReadTimeout, ConnectionError, RequestException try: response = requests.get("http://httpbin.org/get", timeout = 0.5) print(response.status_code) except ReadTimeout: print('Timeout') except ConnectionError: print('Connection error') except RequestException: print('Error') # RequestException # 发送一个模糊的异常 # ConnectionError # 发生连接错误时的异常 # HTTPError # 发生HTTP错误时的异常 # URLRequired # URL错误时的异常 # ConnectTimeout # 连接服务器是请求超时 # ReadTimeout # 服务器没有在指定的时间内发送数据 # Timeout # 请求超时
  • 相关阅读:
    Java 异常
    【转】Java 内部类总结
    【转】Java中的static关键字解析
    【转】事务及事务隔离级别
    【转】事务并发的问题
    【转】hibernate对象三种状态
    iOS指南针
    CoreLocation 定位
    swift 2.2 语法 (下)
    swift 2.2 语法 (中)
  • 原文地址:https://www.cnblogs.com/ming-yuan/p/10226318.html
Copyright © 2020-2023  润新知