zoukankan      html  css  js  c++  java
  • Requests模块—请求

    1. 安装

    pip install requests
    import requests
    

    2. 使用

    (1) GET

    1. 语法
    	requests.get(url, params=None, **kwargs)
        - url:向服务器发送的url
        - params:添加url请求参数
        - proxies[代理ip]
        - headers[请求头信息]
    2. 举例
    	import requests
        # UA伪装,请求载体的身份标识
        headers = {
            'User-Agent':xxx
        }
        url = 'http://douban.com'
        data = {"name":"python"}
        response = requests.get(url=url, params=data, headers=headers)
    
    属性 说明
    response.text 获取响应文本
    response.content 获取网页上的二进制图片、视频
    response.encoding 获取网页编码
    response.encoding="utf-8" 设置网页编码
    response.status_code 获取响应状态码
    response.headers 获取响应头信息
    response.cookies 获取cookies信息
    response.url 获取url信息
    response.history 获取history信息

    (2) POST

    1. 语法格式
    	requests.post(url, data=None, json=None, **kwargs)
        - url:向服务器发送url请求
        - data:提交表单数据
    2. 案例
    	import requests
        url = 'https://baidu.com'
        headers = {
            'User-Agent':xxx
        }
        form_data = {'name':'py', 'pwd':'123'}
        response = requests.post(url=url, data=form_data,headers=headers)
    
  • 相关阅读:
    Linux
    python中元类
    POJ 1182 食物链
    POJ 1733 Parity game
    top 介绍
    周记 2014.8.31
    windows10配置python
    oracle执行update时卡死问题的解决办法
    An Easy Introduction to CUDA C and C++
    super()
  • 原文地址:https://www.cnblogs.com/hq82/p/10792459.html
Copyright © 2011-2022 走看看