高级用法:

会话对象:

我们来跨请求保持一些cookie:



import requests
import json
import re
s=requests.session()
print s.headers

C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a12.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}

Process finished with exit code 0



import requests
import json
import re
s=requests.session()
print s.headers

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Origin': 'http://www.v2ex.com',
'Referer': 'http://www.v2ex.com/signin',
'Host': 'www.v2ex.com',
}
s.headers.update(headers)
print s.headers



C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a12.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}
{'Origin': 'http://www.v2ex.com', 'Accept-Encoding': 'gzip, deflate', 'Host': 'www.v2ex.com', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', 'Connection': 'keep-alive', 'Referer': 'http://www.v2ex.com/signin'}

Process finished with exit code 0