# -*- coding: utf-8 -*- """ @Time: 2018/1/12 @Author: songhao @微信公众号: zeropython @File: index.py """ import requests import re def get_title(html): """" :param html: 源代码 :return: 返回源代码的title """ return re.search("<title>(.*?)</title>", html, re.S).group(1) def get_login(url, data): """ :param url: wordpress 后台登录地址 :param data: post传入的账号密码 :return: 返回已经登录的Session对象 """ s = requests.Session() s.post(url, data=data) return s if __name__ == '__main__': # 后台登录地址 login_url = "http://mac-wordpress.test/wp-login.php" # 登录的账号密码 data = {"log":"root", "pwd":"root"} # 登录wordpress s = get_login(login_url, data) # 利用session 请求后台的网址 r = s.get("http://mac-wordpress.test/wp-admin/upload.php") # 获取请求地址的标题 t = get_title(r.text) print(t)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/12
@Author: songhao
@微信公众号: zeropython
@File: index.py
"""
import
requests
import
re
def
get_title
(
html
)
:
""""
:param html: 源代码
:return: 返回源代码的title
"""
return
re
.
search
(
"<title>(.*?)</title>"
,
html
,
re
.
S
)
.
group
(
1
)
def
get_login
(
url
,
data
)
:
"""
:param url: wordpress 后台登录地址
:param data: post传入的账号密码
:return: 返回已经登录的Session对象
"""
s
=
requests
.
Session
(
)
s
.
post
(
url
,
data
=
data
)
return
s
if
__name__
==
'__main__'
:
# 后台登录地址
login_url
=
"http://mac-wordpress.test/wp-login.php"
# 登录的账号密码
data
=
{
"log"
:
"root"
,
"pwd"
:
"root"
}
# 登录wordpress
s
=
get_login
(
login_url
,
data
)
# 利用session 请求后台的网址
r
=
s
.
get
(
"http://mac-wordpress.test/wp-admin/upload.php"
)
# 获取请求地址的标题
t
=
get_title
(
r
.
text
)
print
(
t
)
|
输出: