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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python实现简单遗传算法(SGA)

Python实现简单遗传算法(SGA)

本文用Python3完整实现了简单遗传算法(SGA) Simple Genetic Alogrithm是模拟生物进化过程而提出的一种优化算法。SGA采用随机导向搜索全局最优解或者说近似...

python操作mongodb根据_id查询数据的实现方法

本文实例讲述了python操作mongodb根据_id查询数据的实现方法。分享给大家供大家参考。具体分析如下: _id是mongodb自动生成的id,其类型为ObjectId,所以如果需...

Python生成个性签名图片获取GUI过程解析

Python生成个性签名图片获取GUI过程解析

这篇文章主要介绍了Python生成个性签名图片获取GUI过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先来看看程序运行的样子...

跟老齐学Python之一个免费的实验室

跟老齐学Python之一个免费的实验室

在学生时代,就羡慕实验室,老师在里面可以鼓捣各种有意思的东西。上大学的时候,终于有机会在实验室做大量实验了,因为我是物理系,并且,遇到了一位非常令我尊敬的老师——高老师,让我在他的实验室...

python自动翻译实现方法

本文实例讲述了python自动翻译实现方法。分享给大家供大家参考,具体如下: 以前学过python的基础,一般也没用过。后来有一个参数表需要中英文。想了一下,还是用python做吧。调用...