• requests爬虫详解


    Requests

    安装
     

    pip install requests



    示例

    1. from fake_useragent import UserAgent
    2. import requests
    3. def cra1_1():
    4. url = 'http://xx/front/website/findAllTypes'
    5. headers = {'User-Agent': UserAgent().chrome}
    6. resp = requests.get(url, headers=headers)
    7. result = resp.json()
    8. if __name__ == '__main__':
    9. cra1_1()          

    发送请求

    GET请求

    resp = requests.get(url,headers= headers,params=params1) #headers,params1是字典



    POST请求

    resp = requests.post(url,headers=headers,data=data) #headers,data是字典

    获取响应信息

    获取响应信息
    resp.status_code  获取状态码
    resp.text    获取响应内容 (以字符串)
    resp.json()    获取响应内容【python数据,可直接用jsonpath解析】
    resp.content    获取响应内容(以字节的方式)
    resp.headers    获取响应头内容
    resp.url    获取访问地址
    resp.encoding    获取网页编码
    resp.request.headers    请求头内容
    resp.cookie    获取cookie
            

    功能

    代理访问

    1. proxies = {"http": "http://10.10.1.10:3128","https": "https://10.10.1.10:1080",}
    2. requests.get("http://www.zhidaow.com", proxies=proxies)



    设置超时时间

    requests.get('http://github.com', timeout=0.001)



    session自动保存cookies

    1. s = requests.Session() # 创建一个session对象
    2. s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') # 用session对象发出get请求,设置cookies



    ssl验证

    1. requests.packages.urllib3.disable_warnings() # 禁用安全请求警告
    2. resp = requests.get(url, verify=False, headers=headers)
    3.           


            

  • 相关阅读:
    FMEA:总监和架构师都在用的高可用架构分析方法
    打印机 检测到用过的耗材或者赝品耗材
    孤儿进程,守护进程,僵尸进程
    Excel找回打开密码过程
    Pytorch 基于LeNet的手写数字识别
    网络编程day02
    自定义MVC原理
    【Oracle】查看存储过程编译错误信息
    Linux开发板安装Ubuntu标准桌面环境(或其他桌面环境)
    Mybatis面试题(三)
  • 原文地址:https://blog.csdn.net/m0_63040701/article/details/133187482