本文实例讲述了python常用模块之requests模块用法。分享给大家供大家参考,具体如下:
一. get请求
1.访问一个页面
1
2
3
4
|
import requests r = requests.get( 'http://www.so.com' ) print (r.status_code) print (r.text) |
2.带参数
1
2
3
4
|
import requests params = { 'a' : 1 , 'b' : 2 } r = requests.get( 'http://www.so.com' , params = params) print (r.url) |
3.返回数据显示
1
2
3
4
5
6
|
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.请求头
1
2
3
4
5
|
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.传参
1
|
r = requests.post( 'http://www.so.com' , data = { 'fdsafdfs' : 'fsdsfa' , 'fdsfs' : 'dfsfs' }) |
2.传json
1
2
|
params = { 'key' : 'value' } r = requests.post(url, json = params) |
3.传文件
1
2
|
upload_files = { 'file' : open ( '234.txt' , 'rb' )} r = requests.post(url, files = upload_files) |
4.带cookie
1
2
3
|
url = 'http://www.so.com' cs = { 'lalala' : 'lalala' , 'lallala' : '23232' } r = requests.get(url, cookies = cs) |
5.超时
1
|
r = requests.get(url, timeout = 5 ) |
详细用法:
http://docs.python-requests.org/zh_cn/latest/user/quickstart.html
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/u013205877/article/details/89069815