#
#---author:wuhao
#
#在QQ群看到有群友在模擬登陸 pivix.cn 這個網站,閑來無事,我也寫了一個測試一下,起初我把它想的復雜了,認為我需要獲取服務器返回過來的Set-Cookie,然后傳給我post的
地址,在這廢了很大功夫,畢竟在模擬注冊YY是踩過這個坑,后來才發現根本沒這么復雜,這是一個入門型的模擬登陸,連post的數據都沒加密。下面是代碼
import urllib.request
import urllib.parse
import http.cookiejar
import re
BASE_URL="https://www.pixiv.net/"
ToGetKeyURL="https://accounts.pixiv.net/login?lang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index"
postURL="https://accounts.pixiv.net/api/login?lang=zh"
Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"
Origin="https://accounts.pixiv.net"
Referer="https://accounts.pixiv.net/login?lang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index"
Host="accounts.pixiv.net"
#定義自己的opener
def MyOpener():
cookie=http.cookiejar.CookieJar()
cookieHandle=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(cookieHandle)
header = \
{
"User-Agent": Agent,
"Origin": Origin,
"Referer": Referer,
"Host": Host
}
head = []
for key, value in header.items():
head.append((key, value))
opener.addheaders = head
return opener
#獲取需要post的數據postkey
def getpostKey(opener,url):
postKey=""
content=opener.open(url).read().decode("utf-8")
postkey=re.findall(r"<input.+?name=\"post_key\".+?>",content)[0].split(" ")[3].split("\"")[1]
print(postkey)
return postkey
#模擬登陸
def postData(opener):
getInfo=getpostKey(opener, ToGetKeyURL)
post_data=\
{
"pixiv_id":"2857791258@qq.com",
"password":"54shuaige",
#"captcha":"",
#"g_recaptcha_response":"",
"post_key":getInfo,
#"source":"pc",
#"ref":"wwwtop_accounts_index",
#"return_to":"https://www.pixiv.net/",
}
post_data=urllib.parse.urlencode(post_data,"utf-8")
print(post_data.encode("utf-8"))
if opener.open(postURL,post_data.encode("utf-8")).getcode()==200:
print("Login Successfully")
return True
else:
print("Login failed")
return False
# main()主函數進行對定義的函數調用
if __name__=="__main__":
opener=MyOpener()
if postData(opener):
print(opener.open("https://www.pixiv.net/member.php?id=24357167").read().decode("utf-8"))