赞
踩
官方文档:http://www.python-requests.org/en/master/
第三方中文文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
pip install requests
import requests
如果你依然在编程的世界里迷茫,
不知道自己的未来规划,
对python感兴趣,
这里推荐一下我的学习交流圈QQ群:895 797 751,
里面都是学习python的,
import requests
from requests.cookies import RequestsCookieJar
r = requests.get('https://www.baidu.com')
r = requests.get('https://www.baidu.com') ,params={'id': 'abc'})
print(r.url) # 输出:https://www.baidu.com/?id=abc;
r = requests.get('https://www.baidu.com')
print(r.text)
import requests
from requests.cookies import RequestsCookieJar
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload )
print(r.text)
# 输出打印中的headers中是"Content-Type": "application/x-www-form-urlencoded",所以我们可以知道application/x-www-form-urlencoded是默认的post请求的Content-Type
import requests
from requests.cookies import RequestsCookieJar
r = requests.get('https://www.baidu.com', timeout=0.001)
print(r)
# 执行代码时会报错,因为在0.001秒时服务器响应还没有完成
获取cookie from requests.sessions import Session # 返回一个会话的上下文管理器对象 requests = Session() headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'} date = {'ck':'', 'name':'xxxxxxxx', 'password': 'xxxxxxxx', 'remember':'false', 'ticket': ''} result = requests.post('https://accounts.douban.com/j/mobile/login/basic', data=date, headers=headers) print(result.text) # 拿到cookie cookies = result.cookies for i in cookies: print(i) for item in cookies.iteritems(): print(item) print(cookies.get_dict()) print(cookies.list_paths()) print(cookies.list_domains()) print(cookies.items()) 获取使用cookie请求登录页面 from requests.cookies import RequestsCookieJar import requests jar = RequestsCookieJar() jar.set('bid', 'OvpnenMUNWo', domain='.douban.com', path='/') jar.set('dbcl2', r'"153039786:aYRnqJukECk"', domain='.douban.com', path='/') r = requests.get('https://www.douban.com/', cookies=jar) print(r.text)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。