python 获取链接跳转过程中的url、header、cookie
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到。可以说,Requests 完全满足如今网络的需求。
import requests
# 请求链接
url = "http://technology.sharespace.top/"
# 请求头,这里我设置了浏览器代理
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
# 发起请求
response = requests.get(url, headers=headers)
# 输出跳转过程中的url header cookie
for resp in response.history:
print(resp.url)
print(resp.headers)
for cookie in resp.cookies:
print(cookie)
# 输出最终跳转的url header cookie
print(response.url)
print(response.headers)
for cookie in response.cookies:
print(cookie)

