博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3简单使用requests
阅读量:7054 次
发布时间:2019-06-28

本文共 1070 字,大约阅读时间需要 3 分钟。

  官方文档:

  我这里使用的是当前最新的python3.6。

  安装

    pip3 install requests

  

  使用requests模块完成各种操作

  1、get请求

  

import requestsurl='https://www.baidu.com'r = requests.get(url) print(r.status_code)

  2、post请求

url = 'https://www.baidu.com' data_post = 'just put your data and use original format' r = requests.post(url, data=data_post, verify=True) print(r.status_code)

  3、使用代理

import requestsurl='http://docs.python-requests.org/en/master/'proxies={    'http':'127.0.0.1:8080',    'https':'127.0.0.1:8080'}r = requests.get(url,proxies=proxies)print(r.status_code)

  4、自定义header和cookie,获取cookie

 

url = 'https://weixin.sogou.com/weixin?type=1&s_from=input&query=python&ie=utf8&_sug_=n&_sug_type_='        headers = {            'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',            'Cookie': 'name=JSESSIONID;value=aaaUrhXY8CzPBgs1eXUFw;domain=weixin.sogou.com'        }        r = requests.get(url, headers=headers)        # 获取cookie        print(r.cookies)        print(r.status_code)        # print(r.text)

 

转载地址:http://jnlol.baihongyu.com/

你可能感兴趣的文章
java及java web学习笔记
查看>>
SpringMVC+Spring+hibernate整合及分页
查看>>
OpenAI教程
查看>>
LeetCode:459. Repeated Substring Pattern
查看>>
Database Resource website
查看>>
牛客寒假6-E.海啸
查看>>
linq 读取xml
查看>>
const 总结
查看>>
@RestController注解下返回到jsp视图页面
查看>>
搜索框请输入关键字 onfocus 和 onblur
查看>>
随手记:IDAPro蛮强大
查看>>
maven的下载以及安装
查看>>
数组排序
查看>>
前端性能优化-----转发的
查看>>
CentOS 7 开放防火墙端口 命令
查看>>
深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
查看>>
HDU 1181 变形课 【DFS】
查看>>
MySQL事务
查看>>
7月26日实习日志
查看>>
Django之 路由系统
查看>>