requests库的小技巧
#coding:utf-8 import requests # url = 'http://www.baidu.com' # response = requests.get(url) # print (response.cookies) # print (type(response.cookies)) # 将cookjar转换成字典格式的cookies # dict_cookies = requests.utils.dict_from_cookiejar(response.cookies) # print (dict_cookies) # print (type(dict_cookies)) # jar_cookies = requests.utils.cookiejar_from_dict(dict_cookies) # print (jar_cookies) # print (type(jar_cookies)) ######################################关闭ssl认证 # CA认证是老外搞得,我们自己有一套,但是老外不承认,我们请求时就关闭ca认证,verify=False # url = 'https://www.12306.cn/mormhweb/' # try: # response = requests.get(url) # except: # response = requests.get(url, verify=False) # print (response.content) ######################################超时 # url = 'http://www.youtube.com' # # response = requests.get(url, timeout=3)
# 断言
# 判断语句是否为真:为真,过;不为真,抛出异常
# assert response.status_code == 200