python模拟登陆 pixiv

#
#---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"))

 

posted @ 2017-04-25 20:43  一光年太远  阅读(3889)  评论(3编辑  收藏  举报