Python常用模块之requests模块用法分析

yipeiwu_com5年前Python基础

本文实例讲述了Python常用模块之requests模块用法。分享给大家供大家参考,具体如下:

一. GET请求

1.访问一个页面

import requests
r=requests.get('http://www.so.com')
print(r.status_code)
print(r.text)

2.带参数

import requests
params = {'a':1,'b':2}
r=requests.get('http://www.so.com', params=params)
print(r.url)

3.返回数据显示

import requests
r = requests.get('https://pullwave.com/pw2/api/acc_query_words?auth_usr=free_vip&src=s0&w1=%E6%8A%96%E9%9F%B3&w2=&date_end=2019-4-6&json=1')
print(r.content)
print(r.text)
print(r.json())
print(r.headers)

4.请求头

import requests
r = requests.get('https://pullwave.com/pw2/api/acc_query_words?auth_usr=free_vip&src=s0&w1=%E6%8A%96%E9%9F%B3&w2=&date_end=2019-4-6&json=1', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})
print(r.content)
print(r.text)
print(r.json())

二.POST请求

1.传参

r = requests.post('http://www.so.com', data={'fdsafdfs': 'fsdsfa', 'fdsfs': 'dfsfs'})

2.传json

params = {'key': 'value'}
r = requests.post(url, json=params)

3.传文件

upload_files = {'file': open('234.txt', 'rb')}
r = requests.post(url, files=upload_files)

4.带cookie

url = 'http://www.so.com'
cs = {'lalala': 'lalala', 'lallala': '23232'}
r = requests.get(url, cookies=cs)

5.超时

r = requests.get(url, timeout=5)

详细用法:
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

用openCV和Python 实现图片对比,并标识出不同点的方式

用openCV和Python 实现图片对比,并标识出不同点的方式

最近项目中需要实现两组图片对比,并能将两者的区别标识出来。 在网上搜索一大堆找到一篇大神的文章,最终实现该功能,在这里记录下: 想要实现此demo,首先我们得确保电脑上已安装 openC...

Python获取时间戳代码实例

1、获取秒级时间戳与毫秒级时间戳、微秒级时间戳 import time import datetime t = time.time() print (t) #...

Python3中的json模块使用详解

1. 概述 JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式. Python标准库中的json模块提供了JSON数据的处理功能. Pyt...

python计算时间差的方法

本文实例讲述了python计算时间差的方法。分享给大家供大家参考。具体分析如下: 1、问题: 给定你两个日期,如何计算这两个日期之间间隔几天,几个星期,几个月,几年? 2、解决方法: 标...

python画一个玫瑰和一个爱心

节日用心准备的礼物,使用python画玫瑰和爱心,供大家参考,具体内容如下 #!/usr/bin/env python #coding=utf-8 #女生节礼物 import r...