使用的是 urllib cookiejar
这里 以 jira 为例
def getAuthorizationCookie():
url = 'https://jira.amer.XXX.com/login.jsp'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Connection': 'keep-alive',
'Host': 'jira.amer.XXXX.com',
'Referer': 'https://jira.amer.XXXX.com/login.jsp'
}
params = {
'os_username': '[email protected]',
'os_password': 'your_password',
'os_destination': '/issues/?jql=project%20%3D%20SCME%20AND%20issuetype%20in%20subTaskIssueTypes()',
'user_role': '',
'atl_token': '',
'login': 'Log In'
}
data = urllib.parse.urlencode(params).encode('utf-8')
request = urllib.request.Request(url = url,data = data,headers = headers,method = 'POST')
cjar = cookiejar.CookieJar()
# 使用HTTPCookieProcessor创建cookie处理器,并以其为参数构建opener对象
cookie = urllib.request.HTTPCookieProcessor(cjar)
opener = urllib.request.build_opener(cookie)
# 将opener安装为全局
urllib.request.install_opener(opener)
response = urllib.request.urlopen(request)
return cjar
这里获取到 cookie 类 在下一步的操作中 把cookie 给下一次请求 携带访问
def downLoadFIle():
cookie = getAuthorizationCookie()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
get_url = 'https://jira.amer.thermo.com/sr/jira.issueviews:searchrequest-csv-current-fields/temp/SearchRequest.csv?jqlQuery=project+%3D+SCME+AND+issuetype+in+subTaskIssueTypes%28%29'
urllib.request.install_opener(opener)
get_request = urllib.request.Request(get_url)
get_response = urllib.request.urlopen(get_request)
data = get_response.read()
with open('jira11.csv', 'wb') as f:
f.write(data)
这里的方法就是进行了一下 下载操作