'''
常见的响应对象参数和方法
response.url 响应的url
response.status_code 响应的状态码
response.status.headers 响应对应的请求头
response.headers 响应头
response.request.cookies 响应对应请求的cookie
response.cookies 响应的cookie
response.json() 自动将json字符串类型的响应内容转换为python对象
'''
发送带参数的请求
1.url中直接带参数
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
url1 = 'https://www.baidu.com/s?wd=python'
response2 = requests.get(url1, headers=headers)
2.构建参数字典
data = {'wd': 'python'}
response2 = requests.get(ur, headers=headers, params=data)
3.在headers中携带cookies参数
url1 = 'https://www.52pojie.cn'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36', 'Cookie': 'htVD_2132_saltkey=Zapuw93G; htVD_2132_lastvisit=1610019361; __gads=ID=a17319558888e2c6-22f4e83592c50009:T=1610022967:RT=1610022967:S=ALNI_MaOF8om7Pb3IFGLep7grj8ORcA4Qg; htVD_2132_auth=8fd20yZKWU2GHdMxJtZsQXdTyJ547JFoY9zG/dpBs31wMmGKalAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaY4dEKQ08/+; htVD_2132_connect_is_bind=1; htVD_2132_naaaaaaaaaaaaaaaaaaaa1; htVD_2132_smile=1D1; htVD_2132_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaat=1; htVD_2132_sid=0; htVD_2132_aaaaaaaaaaaaaaaaaa2161200; htVD_2132_ulastactivity=1612184871|0; Hm_lvt_46d556462595ed05e05f009cdafff31a=1611742013,1611841484,1612161149,1612184824; htVD_2132_ttask=1020776|20210201; htVD_2132_st_t=1020776|1612184904|6b5a32d11e9718a4df523acc102241b4; htVD_2132_forum_lastvisit=D_24_1610791400D_8_1611232100D_39_1611232737D_2_1611669354D_16_1611736770D_10_1612184884D_66_1612184904; htVD_2132_visitedfid=66D10D8D16D15; htVD_2132_lastcheckfeed=1020776|1612184904; htVD_2132_st_p=1020776|1612184916|a75c9c617eaef2112774581ee6b6968d; htVD_2132_viewid=tid_1363530; Hm_lpvt_46d556462595ed05e05f009cdafff31a=1612184869; htVD_2132_lastact=1612184966 forum.php ajax'}
response2 = requests.get(url1, headers=headers)
with open("wuaipojie_cookie", "wb")as f:
f.write(response2.content)
headers1 = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
response3 = requests.get(url1, headers=headers1)
with open("wuaipojie_nocookie", "wb")as f:
f.write(response3.content)
print(response3.content.decode())
4.使用cookie参数保持会话
# 构建cookie字典
temp = 'htVD_2132_saltkey=Zapuw93G; htVD_2132_lastvisit=1610019361; __gads=ID=a17319558888e2c6-22f4e83592c50009:T=1610022967:RT=1610022967:S=ALNI_MaOF8om7Pb3IFGLep7grj8ORcA4Qg; htVD_2132_auth=8fd20yZKWU2GHdMxJtZsQXdTyJ547JFoY9zG/dpBs31wMmGKalAYx/aKuTwCbNTULZUlVi9Utxm5+PIRAwY4dEKQ08/+; htVD_2132_connect_is_bind=1; htVD_2132_nofavfid=1; htVD_2132_smile=1D1; htVD_2132_atarget=1; htVD_2132_sid=0; htVD_2132_lastviewtime=1020776|1612161200; htVD_2132_ulastactivity=1612184871|0; Hm_lvt_46d556462595ed05e05f009cdafff31a=1611742013,1611841484,1612161149,1612184824; htVD_2132_ttask=1020776|20210201; htVD_2132_st_t=1020776|1612184904|6b5a32d11e9718a4df523acc102241b4; htVD_2132_forum_lastvisit=D_24_1610791400D_8_1611232100D_39_1611232737D_2_1611669354D_16_1611736770D_10_1612184884D_66_1612184904; htVD_2132_visitedfid=66D10D8D16D15; htVD_2132_lastcheckfeed=1020776|1612184904; htVD_2132_st_p=1020776|1612184916|a75c9c617eaef2112774581ee6b6968d; htVD_2132_viewid=tid_1363530; Hm_lpvt_46d556462595ed05e05f009cdafff31a=1612184869; htVD_2132_lastact=1612184966 forum.php ajax'
cookie_list = temp.split('; ')
cookies = {cookie.split('=')[0]: cookie.split('=')[-1]for cookie in cookie_list}
# cookies = {}
# for cookie in cookie_list:
# cookies[cookie.split('=')[0]] = cookie.split('=')[-1]
print(cookies)
response1 = requests.get(url, headers=headers1, cookies=cookies)