欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-09-23 08:45:35|栏目: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爬虫爬取小红书完完整整的全过程

栏    目:Python代码

下一篇:python实现zencart产品数据导入到magento(python导入数据)

本文标题:Python常用模块之requests模块用法分析

本文地址:http://www.codeinn.net/misctech/176876.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有