python 模拟登陆wordpress

# ###########################################################################
# *****使用request +http.cookiejar 保存cookie到文本*****
# from urllib import request
# import http.cookiejar
# url = “https://www.51yy.top/wp-login.php”
# headers = {
# “referer”:”https://www.51yy.top/wp-login.php”,
# “user-agent”:”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0″
# }
# #构造登录请求
# req = request.Request(url,headers=headers)
# #申明cookie 容器,路径是cookies保存路径
# cookie = http.cookiejar.MozillaCookieJar(“cookie.txt”)
# #创建cookie处理器
# handler = request.HTTPCookieProcessor(cookie)
# #构造自己的请求器
# opener = request.build_opener(handler)
# #发起请求
# response = opener.open(req)
# #保存cookie
# cookie.save(ignore_discard=True,ignore_expires=True)
# result = response.read().decode(“UTF-8”)
# print (result)
# #############################################################################
# ******使用requests 获取cookies*******
# import requests
# url = “https://www.51yy.top/wp-login.php”
# headers = {
# “referer”:”https://www.51yy.top/wp-login.php”,
# “user-agent”:”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0″
# }
# session = requests.session()
# response = session.get(url,headers=headers)
# result = response.content.decode()
# cookies = response.cookies
# for cookie in cookies:
# print (cookies)
# ######################################################################################
# #******获取提交提交表单中需要的hidden种类的值*********
# import requests
# from lxml import etree
# # 请求登录页面,获取cookie
# login_index_url = “https://www.51yy.top/wp-login.php”
# login_index_headers = {
#    “referer”:”https://www.51yy.top/wp-login.php”,
# “user-agent”:”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0″
# }
# # 为了保存cookie我们用requests.session进行请求
# session = requests.session()
# login_index_response = session.get(login_index_url,headers=login_index_headers)
# result = login_index_response.content.decode()
# html = etree.HTML(result)
# # 取出redirect_to的值
# redirect_to = html.xpath(r”//input[@name=’redirect_to’]”)[0].attrib
# redirect_to = redirect_to[“value”]
# # 取出testcookie的值
# testcookie = html.xpath(r”//input[@name=’testcookie’]”)[0].attrib
# testcookie = testcookie[“value”]
# # 打印出三个值
# print(redirect_to)
# print(testcookie)
####################################################################
import requests
from lxml import etree
# 请求登录页面,获取cookie
login_index_url = “https://www.51yy.top/wp-login.php”
login_index_headers = {
   “referer”:”https://www.51yy.top/wp-login.php”,
“user-agent”:”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0″
}
# 为了保存cookie我们用requests.session进行请求
session = requests.session()
login_index_response = session.get(login_index_url,headers=login_index_headers)
result = login_index_response.content.decode()
html = etree.HTML(result)
# 取出redirect_to的值
redirect_to = html.xpath(r”//input[@name=’redirect_to’]”)[0].attrib
redirect_to = redirect_to[“value”]
# 取出testcookie的值
testcookie = html.xpath(r”//input[@name=’testcookie’]”)[0].attrib
testcookie = testcookie[“value”]
data = {
“log”: “a*****n”,
“pwd”: “J*********7”,
“wp-submit”: “登录”,
“redirect_to”: redirect_to,
“testcookie”: testcookie
}
login_url = “https://www.51yy.top/wp-login.php”
login_headers = {
    “referer”:”https://www.51yy.top/wp-login.php”,
“user-agent”:”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0″
}
login_response = session.post(login_url,headers =login_headers,data = data )
login_result = login_response.content.decode()
print(login_result)
######################################################################

Add a Comment

邮箱地址不会被公开。 必填项已用*标注