转自:http://blog.csdn.net/foryouslgme/article/details/51822209
首先说一下使用Python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路:
1、对于带token的页面,需要先从最开始的页面获取合法token
2、然后使用获取到的合法token进行后续操作
3、token一般存储的地方有两个:
【一种是携带在cookie中】、【一种是在respose(页面返回)的隐藏表单中】,获取思路类似
释:header是针对服务端有各种限制或特定需求时使用的,一般服务器会进行类似如:X-Requested-With、Content-Length、User-Agent等的验证,所以需要将其以字典的形势发送给服务器
'''
此代码只实现了注册的第一步【手机发送验证码】,主要解决的获取token
具体操作有:获取页面第一次请求时的重要信息,如:cookie与token
'''
import requests
host = "http://10.70.18.33:8083/"
url1 = host + "shopxx-mobile/register.jhtml"
r = requests.get(url1)
token = r.cookies.items()[0][1]
cookies = r.cookies
print(type(token))
print(token)
print(cookies)
print(r.headers)
print(r.url)
url2 = host + "shopxx-mobile/register/send.jhtml"
headers = {
"token": token,
"Host": "10.70.18.33:8083",
"User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"Connection":"keep-alive",
"Content-Length": "18",
"charset":"UTF-8",
"cookie":"token=" + token
}
data = {'mobile':'1851174****'}
s = requests.post(url2,data,headers=headers)
print(s)
print(s.status_code,s.text)
- 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
- 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
执行结果
/Users/frankslg/PycharmProjects/cjb/mobile/regist.py
<class 'str'>
7c74a4cce353aec3133005feb40a9f39
<RequestsCookieJar[<Cookie token=7c74a4cce353aec3133005feb40a9f39 for 10.70.18.33/>]>
{'Date': 'Mon, 04 Jul 2016 08:53:30 GMT', 'Set-Cookie': 'token=7c74a4cce353aec3133005feb40a9f39; Path=/', 'Server': 'Apache-Coyote/1.1', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Language': 'zh-CN', 'Transfer-Encoding': 'chunked'}
http://10.70.18.33:8083/shopxx-mobile/register.jhtml
<Response [200]>
200 {"type":"success","content":"恭喜您,账号注册成功!"}
函数封装
import requests
def reg():
host = "http://10.70.18.33:8083/"
url1 = host + "shopxx-mobile/register.jhtml"
r = requests.get(url1)
token = r.cookies.items()[0][1]
url2 = host + "shopxx-mobile/register/send.jhtml"
headers = {
"token": token,
"Host": "10.70.18.33:8083",
"User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"Connection":"keep-alive",
"Content-Length": "18",
"charset":"UTF-8",
"cookie":"token=" + token
}
data = {'mobile':'1851174****'}
s = requests.post(url2,data,headers=headers)
print(s.status_code,s.text)
if __name__ == '__main__':
reg()