今天使用request的get方法獲取一個網站的登錄頁信息,結果使用charles抓包的時候發現該網站登錄頁303(重定向的問題),網上查了很多資料,原因如下:
一、cookie
原因:利用requests模擬登錄時,直接使用request.get(url),容易造成 301/302/303 重定向,因為cookie不持久,造成重定向之后的cookie等信息獲取不到
1>.同一個Host下邊重定向
解決方法:使用requests.Session()方法,會使該連接持久化,並且保存請求的狀態(session、cookie等),這樣在重定向之后,我們使用requests.cookies就能獲取到cookie了
代碼:
result = requests.Session() header = { 'Host' : 'xxxxx', 'Accept' : 'xxxx', 'User-Agen': 'xxxx' # 這個最重要,建議手動設置(有時候沒有設置,造成獲取數據失敗,本人被坑過......) } response = result.get(url, headers=header)
注意:上面講的重定向是在同一個host下重定向,跨域的重定向用上邊的方法就獲取不到cookie
2>.跨域重定向
示例:A(http://a.kuyu.com/login)->B(http://b.baidu.com/xxxxx)->C(http://b.baidu.com/xxxx/xxxx),類似這種形式,那么使用第一種解決辦法就無法獲取到cookie,這時候就需要變換一種方式來進行獲取
# 獲取請求url域名 def getHost(url): pattern = re.compile(r'(.*?)://(.*?)/', re.S) response = re.search(pattern, url) if response: return {'header':str(response.group(1)).strip(), 'host': str(response.group(2)).strip()} else: return None cookieType = {} # 保存域名對應的cookie def getRedirectCookie(url): locationList = set() cookie = '' resUrl = url # 獲取最后請求url header = { 'User-Agen': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' } try: while True: # 請求 response = requests.get(url, headers=header, allow_redirects=False) # 獲取host hostObj = getHost(resUrl) if hostObj is None: return None # 處理cookie cookie = response.cookies.get_dict() if cookie == {}: pass else: cookieType[str(hostObj['host']).strip()] = json.dumps(cookie) # 保存cookie # 獲取跳轉的url if 'Location' in response.headers.keys(): url = response.headers.get('Location') if not 'http' in url: url = hostObj['header'] + '://' + hostObj['host'] + url # 拼接host域名 resUrl = url if url in locationList: break locationList.add(url) else: break return {'url': str(resUrl), 'content': response.content, 'header': response.headers} except urllib2.URLError, e: if hasattr(e, 'reason'): print '請求失敗,原因:' + e.reason return None
注意:有時候執行帶有重定向的時候會出現:"requests.exceptions.TooManyRedirects: Exceeded 30 redirects."的情況,那么這時候是說重定向次數上限了,也可以使用上邊的這種方式來解決
二、Location(重定向url)
有時候遇到重定向,我們只需要獲取重定向的url即可,這個時候使用上述方法開啟一個保持狀態的長連接,並請求時獲取不到的請求的headers的,那么我們該怎么辦呢?
解決:
result = requests.Session() header = { 'Host' : 'xxxxx', 'Accept' : 'xxxx', 'User-Agen': 'xxxx' # 這個最重要,建議手動設置(有時候沒有設置,造成獲取數據失敗,本人被坑過......) } response = result.get(url, headers=header, allow_redirects=False)
location = response.headers['Location'] # 注意有些header返回的Location中的url是不帶host部分的,需要注意一下
allow_redirects=False的意義為拒絕默認的301/302/303重定向從而可以通過response.headers['Location']拿到重定向的URL。