1. 程式人生 > >Python模擬登入多種實現方式

Python模擬登入多種實現方式

Python模擬登入多種實現方式

基於Python 3.6


#coding:utf-8
import sys
import io
import urllib.request
import http.cookiejar



################## 第一種登陸方式 ##################
################## 直接使用已知的cookie訪問 ##################

# #改變標準輸出的預設編碼
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
# #登入後才能訪問的網站
# url = 'http://www.xxx.com.cn/member/index'

# #瀏覽器登入後得到的cookie,也就是剛才複製的字串
# cookie_str = 'PHPSESSID=go07n3aart4qoflrfe0m3nod42'

# #構造登入請求
# request = urllib.request.Request(url)
# #設定cookie
# request.add_header('cookie', cookie_str)
# #設定請求頭
# request.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36')
# # 請求頁面
# response = urllib.request.urlopen(request)
# print(response.read().decode('utf-8'))




################## 第二種登陸方式 ##################
################## 模擬登入後再攜帶得到的cookie訪問 ##################

# #改變標準輸出的預設編碼
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')


# # 登陸資訊
# # 登入後才能訪問的網站
# url = 'http://www.xxx.com.cn/member/index'
# login_url = "http://www.xxx.com.cn/login/quick_login"
# login_username = "xxx"
# login_password = "xxx"

# #登入時需要POST的資料
# login_data = {
#     "username" : login_username,
#     "password" : login_password
# }
# post_data = urllib.parse.urlencode(login_data).encode('utf-8')

# #設定請求頭
# headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
# #構造登入請求
# req = urllib.request.Request(login_url, headers = headers, data = post_data)


# #構造cookie
# cookie = http.cookiejar.CookieJar()

# #由cookie構造opener
# opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))


# #傳送登入請求,此後這個opener就攜帶了cookie,以證明自己登入過
# resp = opener.open(req)

# #構造訪問請求
# req = urllib.request.Request(url, headers = headers)

# resp = opener.open(req)

# print(resp.read().decode('utf-8'))




################## 第三種登陸方式 ##################
################## 儲存Cookie到檔案 ##################

# #改變標準輸出的預設編碼
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

# # 登陸資訊
# # 登入後才能訪問的網站
# url = 'http://www.xxx.com.cn/member/index'
# login_url = "http://www.xxx.com.cn/login/quick_login"
# login_username = "xxx"
# login_password = "xxx"

# #登入時需要POST的資料
# login_data = {
#     "username" : login_username,
#     "password" : login_password
# }
# post_data = urllib.parse.urlencode(login_data).encode('utf-8')

# #設定請求頭
# headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
# #構造登入請求
# req = urllib.request.Request(login_url, headers = headers, data = post_data)

# #設定儲存cookie的檔案,同級目錄下的cookie.txt
# filename = 'cookie.txt'
# #宣告一個MozillaCookieJar物件例項來儲存cookie,之後寫入檔案
# cookie = http.cookiejar.MozillaCookieJar(filename)

# #由cookie構造opener
# opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))


# #傳送登入請求,此後這個opener就攜帶了cookie,以證明自己登入過
# resp = opener.open(req)
# #儲存cookie到檔案
# cookie.save(ignore_discard=True, ignore_expires=True)

# #構造訪問請求
# req = urllib.request.Request(url, headers = headers)

# resp = opener.open(req)

# print(resp.read().decode('utf-8'))




################## 第四種登陸方式 ##################
################## 從檔案中獲取Cookie並訪問 ##################

#改變標準輸出的預設編碼
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

# 登陸資訊
# 登入後才能訪問的網站
url = 'http://www.xxx.com.cn/member/index'

#設定請求頭
headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
#構造登入請求
req = urllib.request.Request(url, headers = headers)

#設定儲存cookie的檔案的檔名,相對路徑,也就是同級目錄下
filename = 'cookie.txt'
#建立MozillaCookieJar例項物件
cookie = http.cookiejar.MozillaCookieJar()
#從檔案中讀取cookie內容到變數
cookie.load(filename, ignore_discard=True, ignore_expires=True)
#利用urllib.request庫的HTTPCookieProcessor物件來建立cookie處理器,也就CookieHandler
handler = urllib.request.HTTPCookieProcessor(cookie)
#通過CookieHandler建立opener
opener = urllib.request.build_opener(handler)
#此用opener的open方法開啟網頁
response = opener.open(req)
#列印資訊
print(response.read().decode('utf-8'))